file write

 

hi can some body tell me why my file writing code override the existing values?

int handle;
   handle=FileOpen("daily.csv", FILE_CSV|FILE_WRITE, ';');
   if(handle<1)
   {
    Comment("File data1.csv not found, the last error is ", GetLastError());
     return(false);
    }
  else
    {
     FileWrite(handle,"curopendaily",Open[0]);FileWrite(handle,"previousdailyopen",Close[1]);FileWrite(handle,"curweeklyopen",High[7]);
     FileWrite(handle,"previousweeklyopen",High[3]);FileWrite(handle,"curmonthlyopen",Low[1]);FileWrite(handle,"previousmonthlyopen",Time[0]);
    }
    FileClose(handle);
 
ankitkalindi:

hi can some body tell me why my file writing code override the existing values?

You Open the File, you write to it, then you close it. Then you repeat this . . . read the documentation and then explain what you think your code should be doing . . .
 
RaptorUK:
You Open the File, you write to it, then you close it. Then you repeat this . . . read the documentation and then explain what you think your code should be doing . . .

int handle;
   handle=FileOpen("cur.csv", FILE_CSV|FILE_WRITE, ';');
   

   if(handle<1)
   {
      return(false);
   }   
  else{
      FileSeek(handle, 0, SEEK_END);
      FileWrite(handle,"curdailyopen",curdailyopen,"previousdailyopen",previousdailymedian,"curweeklyopen",curweeklyopen,"previousweeklymedian",
               previousweeklymedian,"curmonthlyopen",curmonthlyopen,"previousmedian",previousmedian);
      FileClose(handle);
     
    }

sir i have changed the code but yet the problem has not been resolved

1)it override the values

2)the values are coming in the same column

 
ankitkalindi:

sir i have changed the code but yet the problem has not been resolved

1)it override the values

2)the values are coming in the same column

If you want help please answer my simple question . . .

RaptorUK:
You Open the File, you write to it, then you close it. Then you repeat this . . . read the documentation and then . . . . . explain what you think your code should be doing . . .

 

mothlymedian,weeklymedian

can you tell me .....

 
ankitkalindi:
  handle=FileOpen("cur.csv", FILE_CSV|FILE_WRITE, ';');

sir i have changed the code but yet the problem has not been resolved

1)it override the values

Of course. You open the file for writing, any existing data is now gone.
 
ankitkalindi:

sir i have changed the code but yet the problem has not been resolved

You have only opened to the file for writing (FILE_WRITE). If you check the result of your current call to FileSeek(), I would bet that it is returning false because FileSeek() can't read the file since it has only been opened for writing. You must open the file for both reading (FILE_READ) and writing, so FileSeek() can seek to end-of-file.

handle=FileOpen("cur.csv", FILE_CSV|FILE_READ|FILE_WRITE, ';');

EDIT: Interestingly, FileSeek(handle, 0, SEEK_END) does not return false while the file is opened only for FILE_WRITE, nor does GetLastError() return an error code. If the file is opened only for FILE_WRITE, FileWrite() appears to start at BOF, regardless of FileSeek() to EOF. However, if file is also opened for reading (FILE_READ), FileSeek(handle, 0, SEEK_END) updates pointer to EOF and FileWrite() starts at EOF.

 
Thirteen:

You have only opened to the file for writing (FILE_WRITE). If you check the result of your current call to FileSeek(), I would bet that it is returning false because FileSeek() can't read the file since it has only been opened for writing. You must open the file for both reading (FILE_READ) and writing, so FileSeek() can seek to end-of-file.

EDIT: Interestingly, FileSeek(handle, 0, SEEK_END) does not return false while the file is opened only for FILE_WRITE, nor does GetLastError() return an error code. If the file is opened only for FILE_WRITE, FileWrite() appears to start at BOF, regardless of FileSeek() to EOF. However, if file is also opened for reading (FILE_READ), FileSeek(handle, 0, SEEK_END) updates pointer to EOF and FileWrite() starts at EOF.

Why do you expect FileSeek(handle, 0, SEEK_END) should return false if file is opened with FILE_WRITE only ?

See documentation, a file open with FILE_WRITE only is emptied, so FileSeek(handle, 0, SEEK_END) works normally but the file pointer is already at end and can not be otherwise.

If FILE_WRITE does not combine with FILE_READ, a zero-length file will be opened. If even the file containd some data, they will be deleted.

You can check that behavior by simply opening and closing a file.

  int handle;
  handle=FileOpen("my_data.csv",FILE_CSV|FILE_WRITE,';');
  if(handle<1)
    {
     Print("File my_data.dat not found, the last error is ", GetLastError());
     return(false);
    } 
  FileClose(handle);  

"my_data.csv" file is now empty.

Reason: