C++: Reading an ifstream
-
This bit of code has been running flawlessly for quite a while. The data file is a text file that contains a bunch of white space separated integers that get read into a vector. I allow comments in the data file by preceding the comment with a semicolon. I always had revision history comments at the end of the file but yesterday I added a new one at the end and the whole app stopped working. Can you see the problem?
std::vector<int> targets;
std::ifstream targetfile(FileName.c_str());
if (targetfile.is_open())
{
int number = 0;
targets.push_back(number); // place holder for position zero
while (!targetfile.eof())
{
char next = targetfile.peek();
if (next == ';')
{
// bypass the rest of the line as it is a comment
while (next != '\n')
{
targetfile.get(next);
}
}
else if (isspace(next))
{
// advance pass the white-space
targetfile.get(next);
}
else
{
// grab the number and add it to the vector
targetfile >> number;
targets.push_back(number);
}
}
targetfile.close();
}
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
-
This bit of code has been running flawlessly for quite a while. The data file is a text file that contains a bunch of white space separated integers that get read into a vector. I allow comments in the data file by preceding the comment with a semicolon. I always had revision history comments at the end of the file but yesterday I added a new one at the end and the whole app stopped working. Can you see the problem?
std::vector<int> targets;
std::ifstream targetfile(FileName.c_str());
if (targetfile.is_open())
{
int number = 0;
targets.push_back(number); // place holder for position zero
while (!targetfile.eof())
{
char next = targetfile.peek();
if (next == ';')
{
// bypass the rest of the line as it is a comment
while (next != '\n')
{
targetfile.get(next);
}
}
else if (isspace(next))
{
// advance pass the white-space
targetfile.get(next);
}
else
{
// grab the number and add it to the vector
targetfile >> number;
targets.push_back(number);
}
}
targetfile.close();
}
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
-
This bit of code has been running flawlessly for quite a while. The data file is a text file that contains a bunch of white space separated integers that get read into a vector. I allow comments in the data file by preceding the comment with a semicolon. I always had revision history comments at the end of the file but yesterday I added a new one at the end and the whole app stopped working. Can you see the problem?
std::vector<int> targets;
std::ifstream targetfile(FileName.c_str());
if (targetfile.is_open())
{
int number = 0;
targets.push_back(number); // place holder for position zero
while (!targetfile.eof())
{
char next = targetfile.peek();
if (next == ';')
{
// bypass the rest of the line as it is a comment
while (next != '\n')
{
targetfile.get(next);
}
}
else if (isspace(next))
{
// advance pass the white-space
targetfile.get(next);
}
else
{
// grab the number and add it to the vector
targetfile >> number;
targets.push_back(number);
}
}
targetfile.close();
}
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
A newline at the end of a comment is worth two in the bush.
-- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.
-
Your final line didn't end with a newline character?
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
Yeah, that was it. The solution was to check for the end of file while attempting to bypass the comment line.
// bypass the rest of the line as it is a comment
while (next != '\n' && !targetfile.eof())
{Lesson: always check for EOF when reading a file in a loop.:sigh:
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!