-
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;
}
//----------------------------------------------------
-
February 21st, 2012, 11:41 AM
#2
Re: Processing multiple files from a single directory
Did you try to debug your code?
Victor Nijegorodov
-
February 21st, 2012, 11:42 AM
#3
Re: Processing multiple files from a single directory
Sorry here's the code in the right forum format.
Thanks!
Code:
#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;
}
-
February 21st, 2012, 11:45 AM
#4
Re: Processing multiple files from a single directory
Hi Victor,
Yes I'm using DevC++ and the debug goes through without any errors or warnings.
-
February 21st, 2012, 11:54 AM
#5
Re: Processing multiple files from a single directory
 Originally Posted by Simonkey
Sorry here's the code in the right forum format.
No, it is NOT the right forum format.
The right forum format assume the code have proper indentations, not just the grey background with the header "Code:".
Victor Nijegorodov
-
February 21st, 2012, 11:57 AM
#6
Re: Processing multiple files from a single directory
 Originally Posted by Simonkey
Yes I'm using DevC++ and the debug goes through without any errors or warnings.
I never used DevC++, but any proper Debugger allow you to execute code step-by-step (line-by-line) and to see the values of variables around the place of execution.
Victor Nijegorodov
-
February 21st, 2012, 11:58 AM
#7
Re: Processing multiple files from a single directory
Third time lucky :
Code:
#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;
}
-
February 21st, 2012, 12:18 PM
#8
Re: Processing multiple files from a single directory
Hi Victor,
I usually use cout lines to isolate problems if the debugger doesn't throw back any warnings, but I understand that this is probably bad practice. I'm a casual C++ user, and only really use it on odd occasions.
Would you be able to recommend a non-visual C++ compiler, with a good debugger?
Thanks
-
February 21st, 2012, 12:24 PM
#9
Re: Processing multiple files from a single directory
The debugger doesn't give you any warnings, that's the compiler.
Why does it have to be non-visual (interpreted as no gui)? MSVC contains a very good debugger.
-
February 21st, 2012, 12:25 PM
#10
Re: Processing multiple files from a single directory
 Originally Posted by Simonkey
Would you be able to recommend a non-visual C++ compiler, with a good debugger?
No, sorry!
since the last 12 years I use only Microsoft IDEs...
Victor Nijegorodov
-
February 21st, 2012, 12:40 PM
#11
Re: Processing multiple files from a single directory
Hi SMA,
I'm put off by the cost of Microsoft's Visual C++. The DevC++ compiler is free, so it's great for me as a casual user. I just want to get some basic code sorted, so I can process some files quicker.
-
February 21st, 2012, 12:55 PM
#12
Re: Processing multiple files from a single directory
Victor Nijegorodov
-
February 21st, 2012, 12:56 PM
#13
Re: Processing multiple files from a single directory
MSVC express is free as well. http://www.microsoft.com/visualstudi...itions/express
As far as I understand it DevC++ isn't maintained anymore.
Last edited by S_M_A; February 21st, 2012 at 12:59 PM.
-
February 21st, 2012, 12:57 PM
#14
Re: Processing multiple files from a single directory
 Originally Posted by Simonkey
I usually use cout lines to isolate problems if the debugger doesn't throw back any warnings,
As others have stated, the debugger allows you to run your program a single step at a time. It also allows you to watch variables as each line is executed. It's one of first pieces of a compiler suite that programmers look for -- the debugging facilities.
Here are the problems with cout:
You have to change your program every time you need to "cout" something new. Then you would need rebuild, then run, then if there is a problem, put another cout, rebuild, etc. What if your program was not small, but multiple source files? Where do you start with placing cout's? What if the build time is long instead of seconds?
Secondly, you need to now remove all of those cout statements once you're done. How do you know you removed all of them? How do you know that removing them didn't introduce a bug somewhere (because once you touch the source code and add/remove lines, you can potentially introduce a bug).
Last, it is possible that what you're going to "cout" can crash your program. For example, if you attempted to dereference an invalid pointer:
Code:
SomeType *p;
//...
cout << *p;
If p is not initialized, then all bets are off if as to whether that cout statement will be executed properly.
So learn to use the debugger. It is practically mandatory to learn to use it, as you can't write anything beyond toy programs without it (and even toy programs may need to be debugged).
but I understand that this is probably bad practice. I'm a casual C++ user, and only really use it on odd occasions.
But the code you posted would practically require one of us to debug it using the debugger. So you should be using the very same tools we are using to help you solve the problem.
Would you be able to recommend a non-visual C++ compiler, with a good debugger?
First, you are already using a non-Visual C++ compiler, namely g++. That is the compiler that Dev-C++ is using under the hood, and it is an industry standard C++ compiler. The debugger that g++ uses is gdb, a good (but hard to use as-is) debugger.
The problem is that the Dev-C++ IDE has not been maintained in several years. Instead, get CodeBlocks as the IDE, along with the g++ compiler. You get the visual interface to the gdb debug facilities making gdb easier to use. In addition, you can get the DDD debugger which "visualizes" the gdb debugger.
http://www.gnu.org/software/ddd/
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 21st, 2012 at 01:05 PM.
-
February 21st, 2012, 12:58 PM
#15
Re: Processing multiple files from a single directory
Thanks very much Victor/SMA, I haven't seen these express versions before. I'll give them a go.
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
|