Guys need some help,
I am making a script to read the latest from a text file. the script already does this but I'm kind of lost to adjust some things
He picks up the line by numbytes in fseek, but the data line may vary and numbytes not be accurate, how can I fix this?
And another problem is that the line has, date, time, value, separated by space, how to read that line and put these 3 information in variable?
int tam, i;
// Opens a file for READING TEXT
arq = fopen("temp.txt", "rt");
if (arq == NULL) // If there was an error in opening
printf("Problems opening file\n");
// Read last line text file
fseek(arq, tam, SEEK_END);
i = 1;
result = fgets(Line, 100, arq);
if (result) // If it was possible to read the report online content
printf("%d : %s",i,Line);
char * data1,data2,data3;
fstream file; // If you don't know what std::fstream is, look it up
cout << "Problem opening file.\n";
file.seekg(EOF); // go to the end of file
file.unget(); //go back two chars
char in = file.get();
if(in == '\n')
//get line from file until space is encountered and put it in data1
file.getline(data2,' '); //etc
//send the data to the output stream
cout << "Data: " << data1 << " " << data2 << " " << data3 << endl;
I pretty much typed this up in my browser, not guarranteed to work, but pretty sure it does. Hope this helps!
Thanks cire for the correction.
This (should) work for splitting the data into three vars.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int whichVar = 0;
for(unsignedint i = 0; i < line.size(); i++)
if(!line[i] == ' ')
vars[whichVar] += line[i]; //add the non-space char to the current
whichVar++; //switch the variable being used
This will only work as long as there are no spaces besides the separating ones. This will work if there is only two data members as well. or five, six, or seven, just change the size of the array.