file open error

A

Abhijit

Hello everybody,

I am getting a "segmentation fault" error when i compile the following
piece of code. The program runs fine till it encounters the line of
code saying" FILE_lic=fopen.........". And after that, it terminates
saying segmentation fault. I had included the appropriate header files
also! I couldn't makeout where the problem exists in the program. Can
anyone solve this mistery!


void LICENSEinfo::find()
{
char check[10],vendor[20],date[12];
char uw1[20],uw2[20],mystring[200];
int lic_num;
char filestring[20];

struct dirent *dp, *subdp;
DIR *dir, *subdir;

FILE *FILE_list;
FILE *FILE_lic;

FILE_list = fopen("/tmp/list","w");

// Make "/parent" as the present working directory
chdir ("/parent");

dir=opendir(".");
while ((dp=readdir(dir))!=NULL)
{
if(!strcmp(dp->d_name,".")||!strcmp(dp->d_name,".."))
continue;

subdir=opendir(dp->d_name);
while ((subdp=readdir(subdir))!=NULL)
{
if(!strcmp(subdp->d_name,".")||!strcmp(subdp->d_name,".."))
continue;

if(strstr(subdp->d_name,"_lic")!=0)
{

// read the entire file
strcpy(filestring,subdp->d_name);
FILE_lic = fopen(filestring,"r");
// problem: FILE_lic is reset to 0x0 here.
while (feof(FILE_lic)==0)
{
fgets(mystring,200,FILE_lic);
if((strstr(mystring,"INCREMENT"))||(strstr(mystring,"FEATURE")))
{
sscanf(mystring,"%s%s%s%s%s%d",check,uw1,vendor,uw2,date,&lic_num);
fprintf (FILE_list,"%s \t %s \t %d \n",vendor,date,lic_num);
}
}

}

}
}

}




Thanks in advance
 
M

Moonlit

Hi,

Abhijit said:
Hello everybody,

I am getting a "segmentation fault" error when i compile the following
piece of code. The program runs fine till it encounters the line of
code saying" FILE_lic=fopen.........". And after that, it terminates
saying segmentation fault. I had included the appropriate header files
also! I couldn't makeout where the problem exists in the program. Can
anyone solve this mistery!


void LICENSEinfo::find()
{
char check[10],vendor[20],date[12];
char uw1[20],uw2[20],mystring[200];
int lic_num;
char filestring[20];

struct dirent *dp, *subdp;
DIR *dir, *subdir;

FILE *FILE_list;
FILE *FILE_lic;

FILE_list = fopen("/tmp/list","w");

// Make "/parent" as the present working directory
chdir ("/parent");

dir=opendir(".");
while ((dp=readdir(dir))!=NULL)
{
if(!strcmp(dp->d_name,".")||!strcmp(dp->d_name,".."))
continue;

subdir=opendir(dp->d_name);
while ((subdp=readdir(subdir))!=NULL)
{
if(!strcmp(subdp->d_name,".")||!strcmp(subdp->d_name,".."))
continue;

if(strstr(subdp->d_name,"_lic")!=0)
{

// read the entire file
strcpy(filestring,subdp->d_name);
FILE_lic = fopen(filestring,"r");

In the current directory there isn't a file with the contents of
subdp->d_name. Hence fopen can't find the file and returns 0.

You should first change with chdir to the child directory you are reading
from. Though you opened the subdir for reading with readdir, your working
directory is still set to the parent of the subdir.

Regards, Ron AF Greve.
 
D

Default User

Abhijit said:
Hello everybody,

I am getting a "segmentation fault" error when i compile the following
piece of code. The program runs fine till it encounters the line of
code saying" FILE_lic=fopen.........". And after that, it terminates
saying segmentation fault. I had included the appropriate header files
also! I couldn't makeout where the problem exists in the program. Can
anyone solve this mistery!

FILE_lic = fopen(filestring,"r");
// problem: FILE_lic is reset to 0x0 here.


Do you understand what it means when the return value from fopen() is
0? If not, why not? What book are you using?

Do you understand that FILE_lic is a pointer? Do you understand what
happens if you attempt to use a null pointer? If not, why not?





Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top