About ifstream

J

Joe

Hello all,
I dont know how to fix it. The program is to open a file
"the_obtained_output", then open another file "verification1". The
first file can be opened successfully but I got a error message
"error: unable to open the input file: 000000".

I checked the codes, I found if I removed
"for(float i;infile>>i;)
prediction.push_back(i);
for(vector<float>::size_type ix=0;ix!=10;ix++)
cout<<prediction[ix]<<endl;",
the error message did not display.

Can anybody tell me why? I really dont know the reason.

The codes are listed below,

int main(){
vector<float> prediction, observation;
ifstream infile("the_obtained_output");
if (!infile){
cerr<<"error: unable to open the input file: "<<infile<<endl;
return -1;
}

for(float i;infile>>i;)
prediction.push_back(i);
for(vector<float>::size_type ix=0;ix!=10;ix++)
cout<<prediction[ix]<<endl;

infile.close();
infile.open("verification1");

if (!infile){
cerr<<"error: unable to open the input file: "<<infile<<endl;
return -1;
}
return 0;
}


Thank you, all.
 
J

Jim Langston

Joe said:
Hello all,
I dont know how to fix it. The program is to open a file
"the_obtained_output", then open another file "verification1". The
first file can be opened successfully but I got a error message
"error: unable to open the input file: 000000".

I checked the codes, I found if I removed
"for(float i;infile>>i;)
prediction.push_back(i);
for(vector<float>::size_type ix=0;ix!=10;ix++)
cout<<prediction[ix]<<endl;",
the error message did not display.

Can anybody tell me why? I really dont know the reason.

Personally I don't reuse ifstreams myself. I would just give the 2nd one a
different variable name.
The codes are listed below,

int main(){
vector<float> prediction, observation;
ifstream infile("the_obtained_output");
if (!infile){
cerr<<"error: unable to open the input file: "<<infile<<endl;
return -1;
}

for(float i;infile>>i;)
prediction.push_back(i);
for(vector<float>::size_type ix=0;ix!=10;ix++)
cout<<prediction[ix]<<endl;

infile.close();

ifstream infile2.open("verification1");
 
J

James Kanze

I dont know how to fix it. The program is to open a file
"the_obtained_output", then open another file "verification1". The
first file can be opened successfully but I got a error message
"error: unable to open the input file: 000000".
I checked the codes, I found if I removed
"for(float i;infile>>i;)
prediction.push_back(i);
for(vector<float>::size_type ix=0;ix!=10;ix++)
cout<<prediction[ix]<<endl;",
the error message did not display.
Can anybody tell me why? I really dont know the reason.
Personally I don't reuse ifstreams myself. I would just give
the 2nd one a different variable name.

That's certainly the best solution.

In his case, the problem is simple: he only stops reading the
first file when an input fails. And the failure flag is sticky;
all stream operations effectively become a no-op until it is
cleared. An "infile.clear()" before the second open should do
the trick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top