I'm trying to do some very simple logic to read numbers from a file. The numbers are separated by a line break.
For that purpose I thought about using seekg with a variable offset and directed towards the beginning of the file, but for some reason the number fetched is always the first one. Here's some of the code:
Sorry about the late reply and thanks for your post.
I'm not sure I fully understand your question, this variable is actually a some sort of counter, this program excerpt is part of a loop.
Also, I've been looking around and it seems to me that using a buffer then a getline would be a wiser idea in this case, even though apparently fileread input fetches only the numbers until a blank space or line break.
Problem now is knowing how to update this counter variable knowing the numbers read from the file don't always have the same number of characters (e.g. 8, 8.4, 8.8, 9.2, 9.6, 10, 10.4, 10.8).
My question was trying to imply that you gave us a code fragment that doesn't give use enough information to help you solve your problem.
You used variable1 to seek into the file (you changed the name to counter_variable), but we don't know how it was set or changed. When you asked why you were always reading the first number, we can't help you because we don't know what else is happening in your code. I would suggest including at least entire functions to give us context around your problem.
Also, when you paste code, please enclose it in code tags. They show up as "<>" in the Format box. The code tags make it easier to read the code, and also attaches line numbers to make it easier to comment about the code.
As far as your new code, and your question. You still don't show us how counter_variable is set/modified. And you close the file before doing getline. That will surely fail.
Instead declare a std::string and then do a getline with the string. This is the second link in Texan40's post. This will help you with your varialble-length numbers. It will look something like this:
1 2 3 4 5 6 7 8 9
std::string line;
...
// probably some sort of loop here
{
getline(fileread, line);
variable2 = atof(line.c_str());
// process variable2
}
If you are unfamiliar with it, you can learn about the string class here:
I did what you suggested and it worked very well. Besides, I created some other counter variable that counts and adds the number of characters in the string each step to help updating the get pointer.
The program scheme looks a bit like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main ()
{
string line;
//variable declarations etc, counter_variable gets zero
//program main loop (...)
{
fileread.open ("File.txt", ios::binary);
fileread.seekg (get_counter);
getline(fileread, line);
fileread.close();
variable1=atof(line.c_str());
get_counter+=line.length();
counter_variable++;
}
return 0;
}
Because I'm working with multiple files in fact. I don't think I can use more than one at the same time. I know this makes the program considerably slower, but well...
This is why I wish you had provided more of your code. The most you gave us was
The program scheme looks a bit like this:
If we had seen more of your code, we could have helped you with the multiple file streams right away. Instead it took a number of questions back and forth before we understood what your problem was, and then more more questions about curiosities before we got to the issue of multiple files.
There are a lot of people here who like to help (as long as the asker has put some effort into the problem), but we need something concrete to work from. Posting too much code is usually better than not posting enough. Obviously you can cut out proprietary things and sections that are completely unrelated. But be careful--sometimes those "unrelated" areas have side effects that cause the problems.