assuming that the included file uses namespace std and contains the std library facilities .
i have written a program to write points on the coordinate plane to some input file stream and then use that stream to add those points to a vector<point>
object;
i have written the program that writes to the input file stream and it works well because i could see the points which i added because my file had a .txt extension namely m.txt
here's my code
#include"../dumbheader.h.txt"
struct point {
int x=0;
int y=0;
point(int i,int j):x(i),y(j){}
point(){}
};
istream& operator>>(istream& is, point& p) { // input format accepted (int,int)
char ch1, ch2,ch3;
if (is >> ch1 && ch1 == '(') {
if (!(is >> p.x))
error(" an integer expected \n");
if (!(is >> ch2 && ch2 == ','))
error(" a ',' is expected ");
if (!(is >> p.y))
error(" bad second argument \n");
if (!(is >> ch3 && ch3 == ')'))
error(" ) expected \n");
}
else error(" ( expected ");
return is;
}
ostream& operator<<(ostream& os, const point& p) {
os <<'(' <<p.x << "," << p.y<<')';
return os;
}
vector<point>vp;
void printv(vector<point>v) {
for (auto x : v)
cout << x << endl;
}
void end_of_loop(istream& ist, char term, const string& message)
{
if (ist.fail()) { // use term as terminator and/or separator
ist.clear();
char ch;
if (ist >> ch && ch == term) return; // all is fine
error(message);
}
}
int main()try{
vector<point>pp;
{
ifstream ifs{ "m.txt" };
for (point p; ifs >> p;)
pp.push_back(p);
}
printv(pp);
}
catch (runtime_error& e) {
cout << e.what();
}
i have omitted the code that writes to m.txt lest that i have to repeatedly write the points to the file, however the code works fine and the file has the text: (1,1)(2,2)(3,3)(4,4). the output of the code: ( expected C:\Users\abdua\source\repos\DrillsOnChap10Points\x64\Debug\DrillsOnChap10Points.exe (process 21104) exited with code 0. Press any key to close this window . . . what i don't understand is why i get this message, the file seems good.
i have added a static int to the input function to write how many times it has been called by outputting the integer before error() is called the latter throws a runtime error. the integer is always one past the number of elements in the file , it is as if the input loop tries to read beyond the end of the file. at this point i'm very confused as to why this is the case; because the input file stream should be closed at the end of the it's associated scope_if i understand correctly. edit:
i have changed the scope inside main to
{
ifstream ifs{ "m.txt" };
int i = 1;
for (point p; ifs >> p&&i<=3;i++)
pp.push_back(p);
}
and the code outputs the three point at the beginning. it seems like the problem is with the fourth read; changing the test for i to i<=4 will cause the same problem.
TLDR: The stream ends up in fail state when reading is >> ch1
making it return false, because you have reached the end-of-file (there is no more to read).
When you read a point
you do this
if (is >> ch1 && ch1 == '(') {
...
}
else {
error(" ( expected ");
}
When you use the >>
operator of the ifstream
(technically the more general istream
) it returns the stream itself and then you execute the bool
operator on istream
, which returns false if the stream is in a fail-state (either failbit
or badbit
is set). As mentioned in the istream::operator >>
reference it may end up in this state, if you attempt to read the current input from the stream into an object but that reading fails (e.g. if the input cannot be parsed to that object type).,
In your code, you assume that you got a wrong character when trying to read the (
and it fails, but what really happens is that you are trying to read an input into an object when the stream has reached the end-of-file (i.e. there is no more to read).
You should have a check for this, either in your for-loop, or in your point
parsing, where you let it fail when reaching end-of-file:
if (is >> ch1 && ch1 == '(') {
...
}
else if (is.eof()) {
return is;
}
else {
error(" ( expected");
}
Also... Please, heed to the advice that the other comments mentioned wrt. how to post your question - you're more likely to get an answer if it's easier to replicate your problem. I couldn't copy-paste your code and run it (i.e. it's not a minimal reproducible example).