Hi!
Please, I really need help with a problem.
I need to read numbers (float type) from a file. Numbers are arranged in rows, I don't know how many in each row, there may be a large number of numbers (milions). How can I read each row of numbers, for example to know how many are in each line?
That's because in C++ there is no eoln function, to detect end of line.
My text file can be:
2.345 1 100.5
23.45 12
11.2 100.2345789 1000 1 1 1
2 3 4 5
1
2
7
If I read every row in a string, I can extract from there every number, but if in a row there are many, many numbers, I have a memory restriction problem, so I can't declare a string char s[100 000 000], maybe bigger, for example.
I wrote this code, but I have some problems (except the problem detailed in rows before):
# include <fstream.h>
using namespace std;
long x,i;
long double a;
char s[1000000], *p, b[1000000], d[]=" ";
ifstream f("date.in");
ofstream g("date.out");
int main()
{ do
{ f.get(s,1000000);
f.get();
p=strchr(s,' ');
while (p)
{ i=p-s;
strncpy(b,s,1000000);
b[i]='\n';
a=atof(b);
g<<a<<' ';
strcpy(s,s+i+1);
p=strchr(s,' ');
}
g<<atof(s)<<' '<<endl;
} while (!f.eof());
}
1- atof(b) return 100.235 if my string b is "100.2345789"
2- My code reads an extra row, it reads a null string, in the end of my input file, so it converts it in 0 and writes this 0 in the end of my date.out, like this:
2.345 1 100.5
23.45 12
11.2 100.235 1000 1 1 1
2 3 4 5
1
2
7
0
If somebody is patient enough to read my text , PLEASE help me!
Thanks a lot!!!