hi, this should be a fairly simple question to answer. I have a function in my program that takes the content of a text file and populates an array with all the information. First it has to see how many lines the file has (as it can change) so it can create a dynamic array based on the number of lines.
if(file.is_open() && (get_file_size() > 0)) //get_file_size checks file size
{
while(!file.eof() )
{
getline(file,temp);
x++; //number of lines
}
}
That part works fine, it's this next part that gives me the trouble.
while(!file.eof() )
{
end = 0;
getline(file,temp);
for(int z=0;z<2;z++) //hardcoded for number of fields to get
{
end = temp.find(" ");
data[y][z] = temp.substr(0,end);
temp = temp.substr(end+1);
}
y++;
}
I realize that once it ran through the file getting the number of lines that it ran to the end of the file, so I tried placing this just before the second part to remedy that:
file.seekg (0, ios::beg);
however, it doesn't seem to work, and I'm confused as to why.
I should mention that when I do file.tellg() right before the second part, it says -1 whether I have the seekg there or not.
to make sure seekg was the problem, I tried closing the file just before the second part and reopening the same file under file2, worked great. So I have a working solution, but I know there must be a better, cleaner way. Thanks!
~J