|
-
February 21st, 2012, 11:30 AM
#1
[RESOLVED] Processing multiple files from a single directory
Hi,
I've tried to form together the following code so that I can process multiple files from one user specified directory. However, when I run the program it works great for the first file, but all other files are blank. The problem starts on the following line:
while(FileIn.good())
If anyone can help me solve this, I'd be really greatful!
//---------------------------------------------------
#include <fstream>
#include <iostream>
#include <dirent.h>
#include <sys/stat.h>
using namespace std;
struct dirent *dirp;
struct stat filestat;
string dir, ImportPath, ExportPath, LineData;
DIR *dp;
ifstream FileIn;
ofstream FileSave;
int main()
{
cout << "Please enter the directory to process: " << flush;
getline(cin, dir);
dp = opendir (dir.c_str() );
if (dp == NULL)
{
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir (dp)))
{ //A
ImportPath = dir + "/" + dirp->d_name;
if (stat( ImportPath.c_str(), &filestat)) continue;
if (S_ISDIR( filestat.st_mode )) continue;
ExportPath = dirp->d_name;
FileSave.open(ExportPath.c_str() );
FileIn.open(ImportPath.c_str() );
cout << ExportPath;
if(FileIn.is_open())
{ //B
cout << " open" << endl;
while(FileIn.good())
{ //C
getline (FileIn, LineData);
cout << LineData << endl;
FileSave << LineData;
//I have some more code here to control strings on each line
} //C
FileIn.close();
} //B
else
{
cout << "File is not open" << endl;
}
FileSave.close();
} //A
closedir( dp );
system("PAUSE");
return EXIT_SUCCESS;
}
//----------------------------------------------------
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|