manojg
September 28th, 2005, 11:54 AM
Hi,
I am tring to open and read a list of 10 files stored in a file name "file_list". But it can open first file only. Here is my code:
------------------------------------------------------
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
main()
{
string name;
ifstream in, in_file;
in.open("file_list");
for(int i=0; i<10; i++){
getline(in,name);
in_file.open(name.c_str());
if(in_file.fail())
cout << name << " file can't open" << endl;
else
cout << name << " ok" << endl;
in_file.close();
}
in.close();
return 0;
}
-------------------------------------------------------------
The output is
file0.dat ok
file1.dat file can't open
file2.dat file can't open
file3.dat file can't open
file4.dat file can't open
file5.dat file can't open
file6.dat file can't open
file7.dat file can't open
file8.dat file can't open
file9.dat file can't open
I have closed the file inside the for loop so that each file can be opened by the same ifstream in_file. According to my guess, this should cause problem. However, I can't see any other problem.
Anybody has idea?
Thanks.
I am tring to open and read a list of 10 files stored in a file name "file_list". But it can open first file only. Here is my code:
------------------------------------------------------
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
main()
{
string name;
ifstream in, in_file;
in.open("file_list");
for(int i=0; i<10; i++){
getline(in,name);
in_file.open(name.c_str());
if(in_file.fail())
cout << name << " file can't open" << endl;
else
cout << name << " ok" << endl;
in_file.close();
}
in.close();
return 0;
}
-------------------------------------------------------------
The output is
file0.dat ok
file1.dat file can't open
file2.dat file can't open
file3.dat file can't open
file4.dat file can't open
file5.dat file can't open
file6.dat file can't open
file7.dat file can't open
file8.dat file can't open
file9.dat file can't open
I have closed the file inside the for loop so that each file can be opened by the same ifstream in_file. According to my guess, this should cause problem. However, I can't see any other problem.
Anybody has idea?
Thanks.