Download MetaTrader 5
To add comments, please log in or register
Share your program via Code Base. Gain access to millions of traders!
macpee
287
macpee 2016.03.25 12: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.
Carl Schreiber
5959
Carl Schreiber 2016.03.25 12: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..
Drazen Penic
2408
Drazen Penic 2016.03.25 12: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. 

macpee
287
macpee 2016.03.25 19: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.
Keith Watford
Moderator
7986
Keith Watford 2016.03.25 19: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.
whroeder1
13641
whroeder1 2016.03.25 20: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.
Demos Stogios
184
Demos Stogios 2016.03.28 14: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 

macpee
287
macpee 2016.03.28 18: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

whroeder1
13641
whroeder1 2016.03.28 19: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);
macpee
287
macpee 2016.03.30 12:39  
Thank you very much @WHRoeder for such detailed code, I am grateful. I shall try as you have advised. 
/
To add comments, please log in or register