-
reading file?
Hi;
How can I read a file like below and calculate the contents:
File(abc.txt)
2.0 2.2 4.5
3.0 3.2 2.5
4.0 4.2 3.5
etc....where each row is the value of x1, x2 x3 and values are around 100 for each x1,x2,x3....
The question is how can I read each values from this file and use them for my calculation...?
-
Same as any other text file - fgets() to read each line and sscanf() to pull it apart
-
in this situation, I think that the atoi() function could be useful (someone correct me if i'm wrong)
If your text file contains numbers, atoi() lets you convert them to integer format for use in your program.
Refrence: http://www.iota-six.co.uk/c/i2_feof_fgets_fread.asp
-
if anything it would be atof but the sscanf will stop that.
Code:
fgets(buffer,sizeof buffer,file);
sscanf(buffer,"%f %f %f",&x,&y,&z);
-
use the library "stdlib.h"
or use "fstream.h" if you are using visual c++
Code:
....
openfile(only in read mode)
....
put the contenent of file into array of 2d //for(lines){for(colums) {@function that allocate numbers into array(matrice)}}
....
@===>a function should control integer (exemple:controle ---->10)