Forgive me if I am doing something stupid, I have not worked too much with C.
I am getting a segmentation fault on a parser that I have written. The program is designed to search through a report directory structure, find report files (basically print files) and then add the reports that are contained within those files to a report database.
This parser can be run on the entire report directory or just a subdirectory, by passing in a command line argument.
When I run the program without the command line argument, I encounter a segmentation fault when it goes to open one of the subdirectories (located in the last day's directory). The command I am using to open the file. The guts of the "process directory" function are shown below. It is seg faulting when trying to opendir command (dir_p = opendir (full_dir.c_str())
.
If I run the program just against the problem directory (or from right above the problem directory) it runs just fine, completes and does not seg fault.
I would guess that I am in trouble with memory allocation either through the string that is holding the file name or through the directory pointer. I have checked and the program has appropriate permissions and there does not seem to be anything wrong with the directory that would keep it from being read (e.g. the file can be opened)
Thanks in advance for any help that you can provide.
I am getting a segmentation fault on a parser that I have written. The program is designed to search through a report directory structure, find report files (basically print files) and then add the reports that are contained within those files to a report database.
This parser can be run on the entire report directory or just a subdirectory, by passing in a command line argument.
When I run the program without the command line argument, I encounter a segmentation fault when it goes to open one of the subdirectories (located in the last day's directory). The command I am using to open the file. The guts of the "process directory" function are shown below. It is seg faulting when trying to opendir command (dir_p = opendir (full_dir.c_str())
If I run the program just against the problem directory (or from right above the problem directory) it runs just fine, completes and does not seg fault.
I would guess that I am in trouble with memory allocation either through the string that is holding the file name or through the directory pointer. I have checked and the program has appropriate permissions and there does not seem to be anything wrong with the directory that would keep it from being read (e.g. the file can be opened)
Thanks in advance for any help that you can provide.
Code:
string process_dir (string dir, string date, string base_dir)
{
/*---------------------------------------------------------------*\
This function will process the given directory looking for all
lower directories. When a file is found, it will process the file
through the process file command. If no date is passed, it will
it will compute and return the date. If a date is passed, that
value will be returned by the function
\*---------------------------------------------------------------*/
DIR * dir_p;
struct dirent * dir_entry_p;
struct stat info;
string current_file, child, full_dir;
if (dir == "." || dir == "..")
{
// throw out the directory
return ("");
}
// Display progress information
printf("Current Working Directory = %s/%s\n", base_dir.c_str(), dir.c_str());
if (date == "")
{
// get the date from the current directory
// date = dir.substr(0,2) + "/" + dir.substr(2,2) + "/" + dir.substr(4,4);
date = dir.substr(4,2) + "/" + dir.substr(6,2) + "/" + dir.substr(0, 4);
}
full_dir = base_dir + "/" + dir;
stat(full_dir.c_str(), &info);
dir_p = opendir (full_dir.c_str());
while (NULL != (dir_entry_p = readdir(dir_p)))
{
current_file = dir_entry_p->d_name;
stat((full_dir + "/" + current_file).c_str(), &info);
current_file = dir_entry_p->d_name;
// check to see if the current file is a directory
if (S_ISDIR(info.st_mode))
{
// process the lower directory
process_dir(current_file, date, full_dir);
}
else // this is a regular file
{
/*----------------------------------------------------------------*\
Process the file by opening the file, looking for available
reports in the file, and adding those reports to the report
database.
\*----------------------------------------------------------------*/
process_file(current_file, full_dir, date);
}
}
return (date);
}