Segfault during a call to fopen

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divc
    New Member
    • Jan 2007
    • 5

    Segfault during a call to fopen

    Hi all,
    I've written a C program. I need to write some data into files. I've written some code and I'm getting a segmentation fault during a call to the fopen function. I've given part of the code here.

    FILE *fp ;
    /* ---
    ---
    Some code here
    ---
    --- */
    /* Check for existence of file */
    fname = strdup("Host");
    strcat (fname , server_address) ;
    strcat (fname , ".txt") ;
    exist = access(fname , F_OK) ;

    /*If it doesn't exist, create a new one */
    if (exist == -1)
    {
    fp = fopen(fname , "w") ;
    if (fp == NULL)
    {
    printf ("\nCannot open file\n") ;
    return ;
    }
    else
    {
    fprintf (fp , "%s %s %s %s %s\n" , inf , ib , ob , ie , oe) ;
    fclose(fp) ;
    }
    printf ("\n\nThe program has to be run once again for performing the necessary calculations.\n \n") ;
    return ;
    }
    /* ---
    ---
    Program continued
    ----
    ----
    */

    As u can see, i've checked if fopen returns null. I can't figure out why this is happening. Can anyone help me?
  • dasarath mood
    New Member
    • Nov 2006
    • 11

    #2
    what is fname..???
    have u allocated enough memory for fname..????
    check once memory issues...






    Originally posted by divc
    Hi all,
    I've written a C program. I need to write some data into files. I've written some code and I'm getting a segmentation fault during a call to the fopen function. I've given part of the code here.

    FILE *fp ;
    /* ---
    ---
    Some code here
    ---
    --- */
    /* Check for existence of file */
    fname = strdup("Host");
    strcat (fname , server_address) ;
    strcat (fname , ".txt") ;
    exist = access(fname , F_OK) ;

    /*If it doesn't exist, create a new one */
    if (exist == -1)
    {
    fp = fopen(fname , "w") ;
    if (fp == NULL)
    {
    printf ("\nCannot open file\n") ;
    return ;
    }
    else
    {
    fprintf (fp , "%s %s %s %s %s\n" , inf , ib , ob , ie , oe) ;
    fclose(fp) ;
    }
    printf ("\n\nThe program has to be run once again for performing the necessary calculations.\n \n") ;
    return ;
    }
    /* ---
    ---
    Program continued
    ----
    ----
    */

    As u can see, i've checked if fopen returns null. I can't figure out why this is happening. Can anyone help me?

    Comment

    • yogesh_anand
      New Member
      • Apr 2006
      • 26

      #3
      Hi
      I tried ur program.Might be u r not creating fname properly.Better new up memory for fname and do forgot to delete in end.
      Where u r using strcpy ,use strncpy() func.

      i tested ur program like this...It's running fine...

      #include<iostre am>
      #include<fstrea m.h>
      using namespace std;
      void main()
      {
      FILE *fp ;
      int exist;
      char *fname=new char[30];
      char *server_address =new char[4];
      strncpy(server_ address,"mib",4 );
      /* Check for existence of file */
      fname = strdup("Host");
      strcat (fname , server_address) ;
      strcat (fname , ".txt") ;
      exist = access(fname , F_OK) ;

      /*If it doesn't exist, create a new one */
      if (exist == -1)
      {
      fp = fopen(fname , "w") ;
      if (fp == NULL)
      {
      printf ("\nCannot open file\n") ;
      return ;
      }
      else
      {
      fprintf (fp , "hi hi hi hi hi\n" ) ;
      fclose(fp) ;
      }
      printf ("\n\nThe program has to be run once again for performing the necessary calculations.\n \n") ;
      return ;
      }
      }
      ~




      Originally posted by divc
      Hi all,
      I've written a C program. I need to write some data into files. I've written some code and I'm getting a segmentation fault during a call to the fopen function. I've given part of the code here.

      FILE *fp ;
      /* ---
      ---
      Some code here
      ---
      --- */
      /* Check for existence of file */
      fname = strdup("Host");
      strcat (fname , server_address) ;
      strcat (fname , ".txt") ;
      exist = access(fname , F_OK) ;

      /*If it doesn't exist, create a new one */
      if (exist == -1)
      {
      fp = fopen(fname , "w") ;
      if (fp == NULL)
      {
      printf ("\nCannot open file\n") ;
      return ;
      }
      else
      {
      fprintf (fp , "%s %s %s %s %s\n" , inf , ib , ob , ie , oe) ;
      fclose(fp) ;
      }
      printf ("\n\nThe program has to be run once again for performing the necessary calculations.\n \n") ;
      return ;
      }
      /* ---
      ---
      Program continued
      ----
      ----
      */

      As u can see, i've checked if fopen returns null. I can't figure out why this is happening. Can anyone help me?

      Comment

      • divc
        New Member
        • Jan 2007
        • 5

        #4
        fname is a character pointer. I've initialized it as char *fname = NULL. Now, if i use fname = malloc(sizeof(c har[30])) in my program as suggested above, the program works just fine. But the issue here is that I've used other strings in the program and i've not explicitly used malloc. I've only used strdup("") during initialization. I didn't have any problems with those other variables. Y am i having a memory problem with this one ?

        Comment

        • yogesh_anand
          New Member
          • Apr 2006
          • 26

          #5
          Hi

          You have declare poniter fname and intial it also.But u haven't allocated memory for that one.Thats why it's coring.Intial pointer to null allocate memory for it also.Either using new or malloc it's up to u.
          Once u will intial sufficient or appropriate memory blocks it will not going to core(as shown by me in sample program).
          After that u can use strdup or strncpy.I will suggest strncpy.

          Originally posted by divc
          fname is a character pointer. I've initialized it as char *fname = NULL. Now, if i use fname = malloc(sizeof(c har[30])) in my program as suggested above, the program works just fine. But the issue here is that I've used other strings in the program and i've not explicitly used malloc. I've only used strdup("") during initialization. I didn't have any problems with those other variables. Y am i having a memory problem with this one ?

          Comment

          • dasarath mood
            New Member
            • Nov 2006
            • 11

            #6
            you malloc() a limited buffer using strcat(), but if the string is longer than u allocated memory for fname, the buffer will not be big enough. use strncat, and calculate the remaining space for the strncat() appropriately every time .

            u will not get any problem after this.always use strncat ,it solves memory problems.

            Comment

            Working...