Reading text file - page 2

 
devilian1899 wrote >>

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

In my case I don't know how many paragraphs file has or how long each paragraph.

In your case it is very simple:

int start()
{
int val[3][3],i,j,handle;
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
for(i=0;i<3;i++)
  for(j=0;j<3;j++)
   {
    val[i][j]=StrToInteger(FileReadString(handle));//read one variable
    //Print("Val",i,j," - ",val[i][j]);//You can print it to be sure
   } 
FileClose(handle); //close file
return(0);    
}
 
Roger:

In my case I don't know how many paragraphs file has or how long each paragraph.

In your case it is very simple:

Hello Roger,


Did you say that your previous example will working no matter how many are the paragraphs? In my real case, I don't know how many paragraphs is in the text file. I guess I need to re-read your code.


Thank you

 

Hi Roger,

Thanks for the code. Im using it to read a text file in the form of "22 43 4554 123 12" for instance, but what I really need is to read a file in CSV format.

Ive tried replacing

handle=FileOpen("test.txt",FILE_READ)

with

handle=FileOpen("test.csv",FILE_READ|FILE_CSV,',')

but apparently only the final value is read. Im sure Im missing something...

 
Thanks for the code. Im using it to read a text file in the form of "22 43 4554 123 12"

.

Try

handle=FileOpen("test.csv",FILE_READ|FILE_CSV,' ') ; // last parameter is a space, as it appears your delimiter is a space

 

Is there some reason the space " " can not be specified as delimiter in FileOpen()? Nobody seemed to have suggested this.


[Edit:] it has, i can see it now after drinking some coffee.

 
supertrade:

handle=FileOpen("test.csv",FILE_READ|FILE_CSV,',')

but apparently only the final value is read. Im sure Im missing something...

You cannot tell it to look for a comma when there are no commas in the file. makes no sense.
 

Hello all. I have a similar issue and hope you can help me with it. 

I want to read all the elements of an array in a file. EX: "file.text" that contains (1,2,3,4,5,6,7)

After reading them, also I want to save those values into an array in the routine to modify them in a( for loop). 

At the end of the loop, I want to save them again tothe "file.text" for later use and add one more slot to the array. EX: modified values after the for loop (10,20,30,40,50,60,70,new slot starting at =1)


If anyone can help me to solve this simple task, I will appreciate it a lot.

Thanks in advance. 

 

Simple task ?

Why do they always say that ?

 
Because they are begging. It's easy to beg on the streets for a Dollar, it's not for a thousand.
          Is it easy (or difficult) to …? - General - MQL5 programming forum
 
Marco vd Heijden:

Simple task ?

Why do they always say that ?

As a first step of bargain (as in : it takes a minute to do that and is hence bellow the usual job price scale and it must be done for free :))
Reason: