Problem Creating and reading a file

 

I am creating a code to create and read a TXT file, but for some reason when I try to read it, not works.

Some help will be good.


Thanks in Adavance.

bool today = false;
void OnTick()
  {
   int Num = 5;
   string Spreadsheet = _Symbol+".TXT"; 
   int Myspreadsheethandle;

   
   if (((TimeToString(TimeCurrent(),TIME_MINUTES)) == "16:39") && today == false)
      {                
         Myspreadsheethandle = FileOpen(Spreadsheet,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI);      
         FileSeek(Myspreadsheethandle,0,SEEK_END);         
         FileWrite(Myspreadsheethandle,Num);                                                                             
         FileClose(Myspreadsheethandle);
         today = true;   
      }
      
   int ExtFile = FileOpen(Spreadsheet,FILE_READ|FILE_ANSI|FILE_COMMON,'|',CP_ACP);
   int data = FileReadInteger(ExtFile);
   FileClose (ExtFile);
   
   Comment("Valor    ",data );
}
 

I use this function to add a line to a file:

//use:  addLineToFileCommon("line\n", "folder\\name.txt"); // shared=true, maxPrt=10
bool addLineToFileCommon(const string line, const string fName, const bool shared = true, const int maxPrt=10) {
   ResetLastError();
   static int nErr=0;
   int fH;
   if ( shared )
      fH = FileOpen(fName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON|FILE_SHARE_READ|FILE_SHARE_WRITE);
   else 
      fH = FileOpen(fName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
   if (fH == INVALID_HANDLE ) { if (nErr<maxPrt) Print("Common-File: >"+fName+"< Open FAILED err:",_LastError); return(false);}
   FileSeek(fH,0,SEEK_END);
   FileWriteString(fH, line, StringLen(line) );
   FileClose(fH);
   if (_LastError<2) return(true);
   return(false);
}

and this to read the whole file into an array of the lines:

int readAllLines( const string fName, string &l[], ushort sepLine='\n') { // alternative to FileOpen 
  uchar Bytes[];
  return( FileLoad(fName, Bytes) ? StringSplit(CharArrayToString(Bytes), '\n', l) : 0);
}

With some (not tested!!) changes.

 
Carl Schreiber #:

I use this function to add a line to a file:

and this to read the whole file into an array of the lines:

With some (not tested!!) changes.

Hi, Thanks for help me.

Could you give an example of the file generated?

And how it returns in 

readAllLines

?

Nilson 

 
NFONSECA:

I am creating a code to create and read a TXT file, but for some reason when I try to read it, not works.

Some help will be good.


Thanks in Adavance.

Some help?

 
Carl Schreiber #:

I use this function to add a line to a file:

and this to read the whole file into an array of the lines:

With some (not tested!!) changes.

When I try to use your code a have an error

open FAILED:ERR 5001

Reason: