filewrite(), fileread(), confused

 

Hi,

I am trying to write some filewrite() and fileread() codes. filewrite() like this:

  int Open_Filing()
  { Handle_Open=FileOpen("open.csv", FILE_CSV|FILE_READ, ';');
    if (Handle_Open<0)
      { Handle_Open=FileOpen("open.csv", FILE_CSV|FILE_WRITE, ';');
        Handle_Open=FileOpen("open.csv", FILE_CSV|FILE_READ, ';');
      }
 
    if(Handle_Open>0)
      { Handle_Open=FileOpen("open.csv", FILE_CSV|FILE_WRITE, ';');
        FileWrite(Handle_Open,OrderType(),OrderOpenTime(),OrderSymbol(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());
        FileClose(Handle_Open);
      }
    return(0);
  }

It works. But fileread() like this:

    Read_Open=FileOpen("open.csv", FILE_CSV|FILE_READ, ';');
    if(Read_Open>0)
      { FileSeek(Read_Open, 0, SEEK_SET);
        Open_Order_Type=StrToInteger(FileReadString(Read_Open));
        Open_Open_Time=StrToTime(FileReadString(Read_Open));
        Open_Order_Symbol=FileReadString(Read_Open);
        Open_Open_Price=StrToDouble(FileReadString(Read_Open));
        Stop_Loss=StrToDouble(FileReadString(Read_Open));
        Take_Profit=StrToDouble(FileReadString(Read_Open));
      }
    else
      { Read_Open=FileOpen("open.csv", FILE_CSV|FILE_WRITE, ';');
        Read_Open=FileOpen("open.csv", FILE_CSV|FILE_READ, ';');
      }
    FileClose(Read_Open);

does not work. I know there must be some mistakes but I do not know where they are.

Please help, Thanks!

[Deleted]  

Hi danceWithLions,


for me your code prefectly works, what is not working for you? Do you get compilation errors or is the output incorrect? I mean, of course your snippet is not complete - I just added the missing dataTypeDeclarations to my copy but then it worked...


 

Hi tradeigel


Thanks a lot! I will double check. The output is incorrect.

Happy trading!


 
DanceWithLions wrote >>

I meet the same problem now,the fileread get NULL value,why??

 
Read_Open=FileOpen("open.csv", FILE_CSV|FILE_WRITE, ';');
When that executes, it creates a new empty file. Is that what you want?
 
phy wrote >>
Read_Open=FileOpen("open.csv", FILE_CSV|FILE_WRITE, ';');
When that executes, it creates a new empty file. Is that what you want?

I want open a csv file that haved data,i open it with

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

i found that this code will open a empty file, so i read NULL value with

Print("read=",FileReadString(handle));

how can i open a file with data?

 

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. If there is a need to add data to an existing file, it must be opened using combination of FILE_READ | FILE_WRITE.
If FILE_READ does not combine with FILE_WRITE, the file will be opened only if it already exists. If the file does not exist, it can be created using the FILE_WRITE mode.

I found it does't work like the note in help document

 

" how can i open a file with data?"

to open read-only

handle = FileOpen(fileName, FILE_CSV|FILE_READ, ';')

to open with read/write permission

handle = FileOpen(fileName, FILE_CSV|FILE_READ|FILE_WRITE, ';')

 

the trouble haved been shooted

i have a mistake in my EA