Loading Sequential Files

I'm downloading a a series of filenames and need to load these files into a matrix. When I request a new file name from my matrix of strings, I show an error that the filename has been previously declared (even though I closed it).

I've attached an example with two file reads. I'd like to sequentially read files in with the same string "filename" (myfile in the example below) but changing the content of "filename" to different file titles.

Any help appreciated!

Thanks,

G.



[code]
#include<fstream>
#include<string>
#include<iostream>

using namespace std;

int main(void)
{
string filename="File1.CSV";
ifstream myfile(filename.c_str());


string seg="";
while(!myfile.eof())
{
getline(myfile,seg,'\n');
}
myfile.close();

filename="File2.CSV";
ifstream myfile(filename.c_str()); //***compiler throws error here
// "redelaration of 'st:ifstream myfile"


seg="";
while(!myfile.eof())
{getline(myfile,seg,'\n');
}
myfile.close();

return 0;
}
Last edited on
myfile.open(filename.c_str());

Also, don't loop on eof, but on the reading operation
1
2
while( getline(myfile, seg, '\n') )
   //... 
Thanks - it compiles, but will not output the second file. After opening the second file, it does not appear to read it (I tested to see that the file existed in my program).

What did I do wrong

revised code:

#include<fstream>
#include<string>
#include<iostream>

using namespace std;

int main(void)
{
string filename="File1.CSV";
ifstream myfile(filename.c_str());

if(!filename.c_str())
{
cout<<"File Not Found"<<endl;
return -1;
}

string seg="";
while( getline(myfile, seg, '\n') )
{
cout<<seg<<endl;
}
myfile.close();

filename="File2.CSV";
myfile.open(filename.c_str());
if(!filename.c_str())
{
cout<<"File Not Found"<<endl;
return -1;
}

seg="";
while(getline(myfile, seg, '\n'))
{
cout<<seg<<endl;
}
myfile.close();

return 0;
}
if (!filename.c_str()) should be if (!myfile.is_open()) in both places it occurs.
Just curoius ne555, what makes looping with getline a better idea than end of file?
I corrected the file tests (as above) but still have no data download from the second file opened.

The check myfile.open() returns true, while the check myfile.good() returns false. The program recogonizes the file [(!myfile.is_open()) returns false].

So, I'm still getting no file contents read in on the second file.

Thanks for any help!

Revised Code:

filename="File2.CSV";
myfile.open(filename.c_str());

cout<<"file open? (1/0): "<<myfile.is_open()<<endl;
cout<<"file good? (1/0): "<<myfile.good()<<endl;

if (!myfile.is_open())
{
cout<<"File Not Found"<<endl;
return -1;
}
seg="";
while(getline(myfile, seg, '\n'))
{
cout<<seg<<endl;
}
myfile.close();

return 0;


ps Austin J(289): I believe looping with getline reduces the program by a step and also returns false after the last value. Looping on eof() adds a step and returns an extra value of null at the end which needs to be discarded or disregarded.
I found the answer in previous forums:

two commands clear the file and return the pointer to the beginning:
myfile.clear();
myfile.seekg(0, ios::beg);

(see forum resetting files)

The final code works (I included two pauses to allow the user to see the file recall is working:

#include<fstream>
#include<string>
#include<iostream>

using namespace std;

int main(void)
{
string filename="File1.CSV";
ifstream myfile(filename.c_str());

cout<<"file open? (1/0): "<<myfile.is_open()<<endl;
cout<<"file good? (1/0): "<<myfile.good()<<endl;

char quit='\0';
cout<<"enter any letter to continue";
cin>>quit;


string seg="";
while( getline(myfile, seg, '\n') )
{
cout<<seg<<endl;
}
if (!myfile.is_open())
{
cout<<"File Not Found"<<endl;
return -1;
}
myfile.close();

filename="File2.CSV";
myfile.open(filename.c_str());
myfile.clear();
myfile.seekg(0, ios::beg);

cout<<"file open? (1/0): "<<myfile.is_open()<<endl;
cout<<"file good? (1/0): "<<myfile.good()<<endl;

quit='\0';
cout<<"enter any letter to continue";
cin>>quit;

if (!myfile.is_open())
{
cout<<"File Not Found"<<endl;
return -1;
}
seg="";
while(getline(myfile, seg, '\n'))
{
cout<<seg<<endl;
}
myfile.close();

return 0;
}

Topic archived. No new replies allowed.