file open error

Discussion in 'C++' started by Abhijit, Sep 17, 2004.

  1. Abhijit

    Abhijit Guest

    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
     
    Abhijit, Sep 17, 2004
    #1
    1. Advertising

  2. Abhijit

    Moonlit Guest

    Hi,

    "Abhijit" <> wrote in message
    news:...
    > 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.

    > // 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
     
    Moonlit, Sep 17, 2004
    #2
    1. Advertising

  3. Abhijit

    Default User Guest

    Abhijit wrote:

    > 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
     
    Default User, Sep 17, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    4
    Views:
    517
    Gönen EREN
    Aug 22, 2003
  2. ml
    Replies:
    0
    Views:
    1,455
  3. ml
    Replies:
    2
    Views:
    4,857
    John C. Bollinger
    Nov 30, 2004
  4. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    192
    Rick DeNatale
    Oct 24, 2006
  5. PerlFAQ Server
    Replies:
    0
    Views:
    327
    PerlFAQ Server
    Apr 28, 2011
Loading...

Share This Page