How to read the second line of a text file?

To add comments, please log in or register
Felix
109
Felix  

I have just created an EA that can read the first line of a text file, to obtain a value.

Now, I want to use 2 values: the first one in the first line and the second one in the second line.

My question is how I should modify the source code to read only the second line of the text file?


The actual source code to read a unique first line that I am using is the following:

         ResetLastError();

         int filesignal= FileOpen("excel_pattern_detected.txt",FILE_TXT|FILE_WRITE|FILE_READ);

         if (filesignal != INVALID_HANDLE)

         {            

            content = FileReadString(filesignal);

            FileClose(filesignal);

         }

Thanks.

Carl Schreiber
Moderator
8882
Carl Schreiber  

I would read the file in the common folder in whole and split it into lines:

bool readCsvFile(const string fName, string &allLines[]) {
   int nL, nI, hdl  = FileOpen(fName,FILE_READ|FILE_SHARE_READ|FILE_BIN|FILE_COMMON); // for files in COMMON-folder!!
   if (hdl == INVALID_HANDLE) { Alert(fName," FileOpen in COMMON FAILED: ",_LastError); FileClose(hdl); return(false); }
   string allLines[], singLine[],
          f = FileReadString(hdl,(int)FileSize(hdl));
   FileClose(hdl);
   nL = StringSplit(f,StringGetCharacter("\n",0),allLines);
   return(nL);
   //if needed template code to proceed ..
   while(nL-->0) {
      nI  = StringSplit(allLines[nL],';',singLine);
      while(nI-->0) {  //itm_N = singLine[nI]
         // fore each element of line nL
         ....
      }
   }
   return(nL);
}

This template-function return an array with all lines (2nd line is allLines[1]).

Felix
109
Felix  
Carl Schreiber:

I would read the file in the common folder in whole and split it into lines:

This template-function return an array with all lines (2nd line is allLines[1]).

Thank you very much for the reply. If I find some doubts, I will ask you.

To add comments, please log in or register