File always empty on next open

Back to topics list To post a new topic, please log in or register
avatar
66
macpee 2016.03.25 13:28
 
Each time I read from a file and close the file, the file becomes empty the next time I want to read or write to the file. What mechanism or function can I use to retain the content of the file in order to add to the existing content of the file or read the file again on the next opening of the file. I want the file to retain its content even when I restart the computer. Thank you in advance.
avatar
2017
gooly 2016.03.25 13:29 #
 
macpee:
Each time I read from a file and close the file, the filebecomes empty the next time I want to read or write to the file. Whatmechanism or function can I use to retain the content of the file inorder to add to the existing content of the file or read the file againon the next opening of the file. I want the file to retain its contenteven when I restart the computer. Thank you in advance.
Show the code how you access it..
avatar
164
drazen64 2016.03.25 13:40 #
 

Check documentation on flags for FileOpen() command, especially this part:

 

There are some specific features of work when you specify read and write flags:

If FILE_READ is specified, an attempt is made to open an existing file. If a file does not exist, file opening fails, a new file is not created.

FILE_READ|FILE_WRITE — a new file is created if the file with the specified name does not exist.

FILE_WRITE —  the file is created again with a zero size. 


avatar
66
macpee 2016.03.25 20:41 #
 
drazen64:

Check documentation on flags for FileOpen() command, especially this part:

 

There are some specific features of work when you specify read and write flags:

If FILE_READ is specified, an attempt is made to open an existing file. If a file does not exist, file opening fails, a new file is not created.

FILE_READ|FILE_WRITE — a new file is created if the file with the specified name does not exist.

FILE_WRITE —  the file is created again with a zero size. 



My question is how to add a new record to an existing file which already has some records. I am not reading, I am WRITING (adding records). All you have talked is about a file not existing or file existing or deleting a file and creating a new one. Just assume that the file exists and that the system can see the file. How to add a new record to the file after opening. That is the task. Please read my question carefully before answering @drazen64. Thank you in advance.
avatar
Moderator
3406
GumRai 2016.03.25 20:54 #
 
macpee:
Each time I read from a file and close the file, the filebecomes empty the next time I want to read or write to the file. Whatmechanism or function can I use to retain the content of the file inorder to add to the existing content of the file or read the file againon the next opening of the file. I want the file to retain its contenteven when I restart the computer. Thank you in advance.
macpee:

My question is how to add a new record to an existing file which already has some records. I am not reading, I am WRITING (adding records). All you have talked is about a file not existing or file existing or deleting a file and creating a new one. Just assume that the file exists and that the system can see the file. How to add a new record to the file after opening. That is the task. Please read my question carefully before answering @drazen64. Thank you in advance.
You should write your question more carefully.
avatar
12985
WHRoeder 2016.03.25 21:11 #
 
macpee: I am not reading, I am WRITING (adding records).
There are no mind readers here. We can't possibly know what you are doing. Don't just tell us what you think you are doing, post your code.
avatar
126
Demos 2016.03.28 16:02 #
 
macpee:

My question is how to add a new record to an existing file which already has some records. I am not reading, I am WRITING (adding records). All you have talked is about a file not existing or file existing or deleting a file and creating a new one. Just assume that the file exists and that the system can see the file. How to add a new record to the file after opening. That is the task. Please read my question carefully before answering @drazen64. Thank you in advance.


actually, the answer you got there was the right one. The manual is not really clear about it, but you have to include FILE_READ in order for your file not to get zeroed each time you open it 

avatar
66
macpee 2016.03.28 20:47 #
 

Ok I am sorry if I have sounded ambiguous @drazen64. The code is as follows:

int FetchHandle=FileOpen("FetchFile.txt",FILE_WRITE|FILE_CSV|FILE_TXT);
for (j=1; j<=5; j++)
   {
    FileWrite(FetchHandle, j, FetchContent)
    FileClose(FetchHandle);
   }

The idea is to add a new record each time the program loops. The file closes within the loop, but should retain the initial records at next open. Thank you in advance







avatar
12985
WHRoeder 2016.03.28 21:06 #
 
macpee: idea is to add a new record each time the program loops. The file closes within the loop, but should retain the initial records at next open.
I am not reading, I am WRITING (adding records). No you are not. This create a empty file. This is what drazen64 already told you.
int FetchHandle=FileOpen("FetchFile.txt",FILE_WRITE|FILE_CSV|FILE_TXT);
You need to append to a file (note the FILE_READ|FILE_WRITE and the seek to end)
      int      CREATE   = FILE_WRITE|FILE_TXT|FILE_ANSI;
      int      APPEND   = FILE_READ|CREATE;
      string   fileName = WindowExpertName() + ".DBG";
      HANDLE   handle   = FileOpen(fileName, APPEND);
      if(handle != INVALID_HANDLE){
         FileSeek(handle, 0, SEEK_END);
         FileWrite(handle, s);
         FileClose(handle);

avatar
66
macpee 2016.03.30 14:39 #
 
Thank you very much @WHRoeder for such detailed code, I am grateful. I shall try as you have advised. 
Back to topics list  

To add comments, please log in or register