Reading text file

 

Hello All,


It's me asking for help again.


I have a text file, lets say it's name is "test.txt".

The file is contain "100", "200" and "300" each separated by a space (100 200 300).

Is there any way to get the "200" or "300" with the FileOpen and the FileRead function?


Many thanks

 
I solved the similar task:
int handle, space,i,pos[];
string str,word;
handle=FileOpen("test.txt",FILE_READ);
if(handle==-1)return(0); 
if(FileSize(handle)==0){FileClose(handle); return(0); }
while(!FileIsEnding(handle))
   {
   str=FileReadString(handle);
   if(str!="")
      {
      space=0;
      for(i=0;i<StringLen(str);i++)
         {
         if(StringGetChar(str,i)==32)
            {
            space++;
            ArrayResize(pos,space);
            pos[space-1]=i;
            }
         }
      for(i=0;i<=space;i++)
         {
         if(i==0) word=StringSubstr(str,0,pos[0]);
         else word=StringSubstr(str,pos[i-1]+1,pos[i]-pos[i-1]-1);
         //analize your word
          }
       }
   }
FileClose(handle); 
return(0);
 

Hello Roger,


Thank you.

Although I understand some parts of your code, overall I'm not get in to it very well. Would you please explain each smaller loops?

 
devilian1899 wrote >>

Hello All,

It's me asking for help again.

I have a text file, lets say it's name is "test.txt".

The file is contain "100", "200" and "300" each separated by a space (100 200 300).

Is there any way to get the "200" or "300" with the FileOpen and the FileRead function?

Many thanks

Why don't you try this as a CSV file? You can change spaces with commas for example.

Then you can handle the file's elements more easy if they are numbers.

Just a suggestion...

 
marynarz15:

Why don't you try this as a CSV file? You can change spaces with commas for example.

Then you can handle the file's elements more easy if they are numbers.

Just a suggestion...


Thanks, I'll think about it. But how to get the 2nd or the 3rd variable? I can't find any direct MQL4's function to do it.

 
devilian1899 wrote >>

Hello All,

It's me asking for help again.

I have a text file, lets say it's name is "test.txt".

The file is contain "100", "200" and "300" each separated by a space (100 200 300).

Is there any way to get the "200" or "300" with the FileOpen and the FileRead function?

Many thanks

devilian, what is exact file format? - as different ways to 'get at data'

.

IF format = 3 white space delimited numbers, terminated with CRLF

eg:

100 200 300

123 456 789

...

as an example:

THEN consider that eg: a function that is called with a number of 1|2|3 can read a "file record" and then return number from FileReadNumber() 1|2|3

The filePointer or next Byte to read from, will be mapped to the next "file record":

.

double FileReadNumber( int handle)
Read the number from the current file position before the delimiter. Only for CSV files.
To get the detailed error information, one has to call the GetLastError() function.
Parameters:
handle - File handle returned by the FileOpen() function.
Sample:
  int handle;
  int value;
  handle=FileOpen("filename.csv", FILE_CSV, ';');
  if(handle>0)
    {
     value=FileReadNumber(handle);  //IF can keep knowledge of current position in file or eg, file records are newLine delimited then each 3 reads gives a record..
     FileClose(handle);
    }

 
int handle, space,i,pos[];
string str,word;
handle=FileOpen("test.txt",FILE_READ);//try to open file
if(handle==-1)return(0);// if not exist
if(FileSize(handle)==0){FileClose(handle); return(0); } //if empty
while(!FileIsEnding(handle))//read file to the end by paragraph. if you have only one string, omit it
   {
   str=FileReadString(handle);//read one paragraph to the string variable
   if(str!="")//if string not empty
      {
      space=0;
      for(i=0;i<StringLen(str);i++)
         {
         if(StringGetChar(str,i)==32)// look for spaces (32) only
            {
            space++;//yes, we found one more space
            ArrayResize(pos,space);//increase array
            pos[space-1]=i;//write the number of space position to array
            }
         }//now we have array with numbers of positions of all spaces
      for(i=0;i<=space;i++)//start to read elements of string
         {
         if(i==0) word=StringSubstr(str,0,pos[0]);//the first element of string (in your case it is 100)
         else word=StringSubstr(str,pos[i-1]+1,pos[i]-pos[i-1]-1);//the rest of elements
         //analize your word. I mean you can calculate (StrToInteger or StrToDouble), print or collect to another array
          }
       }
   }
FileClose(handle); //close file
return(0);
 

Hello All,


@fjb, I'm not sure that I understand your explanation, but thank you.


@Roger, thank you for the explanation.

 

HelloAll,


I have another question, what if the text file's format is below :


---------------

100;200;300

101;201;301

102;202;302

---------------


Any help is appreciated


Thank you

 
devilian1899:

HelloAll,


I have another question, what if the text file's format is below :


---------------

100;200;300

101;201;301

102;202;302

---------------


Any help is appreciated


Thank you

You can treat it like a CSV file with delimeter ;

You open it this way:

int File = FileOpen("File.CSV", FILE_READ|FILE_CSV,';');

 
robofx.org:

You can treat it like a CSV file with delimeter ;

You open it this way:

int File = FileOpen("File.CSV", FILE_READ|FILE_CSV,';');


Thanks for the reply. But then how to save each value to a variable?


for example :

val1a=100

val1b=200

val1c=300

val2a=101

val2b=201

val2c=301

val3a=102

val3b=202

val3c=302


Roger's example above seems only for a paragraph, or until a new line feed.


Thanks

Reason: