Segmentation error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sagarbsa
    New Member
    • Sep 2007
    • 5

    Segmentation error

    Hii,
    when I run this program using GCC there is no compilation error but I get segmentation error. Please help me

    #include<stdio. h>
    #include<string .h>

    int main()
    {
    FILE *fp;
    char *filename();

    if ((fp = fopen (filename(),"r" )) == NULL)

    {
    printf ("File could not be opened\n");
    // error_handler() ;
    }
    else
    {
    printf ("file opened");
    }
    }

    char *filename() /* return filename */

    { static char *filenm = ".............. ..........";

    do
    {
    printf ("Enter filename :");
    scanf ("%24s",&filenm );
    // skipgarb();
    }
    while (strlen(filenm) == 0);
    return (filenm);
    }
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Well,here is your problem:

    [CODE="cpp"]char *filename() /* return filename */

    { static char *filenm = ".............. ..........";

    do
    {
    printf ("Enter filename :");
    scanf ("%24s",&filenm );
    // skipgarb();
    }
    while (strlen(filenm) == 0);
    return (filenm);[/CODE]

    You have assigned to filenm a literal string,so it's now pointing to it,then you decide to change that string to other which is probably longer,and there you go,my favorite segmentation error.

    A single question,why is filenm static?

    PS:please use code tags(#button)
    Savage

    Comment

    • sagarbsa
      New Member
      • Sep 2007
      • 5

      #3
      Thanks for the reply. But i am still not getting the output. I added the skipgarb function also. And I am using only a file name of 6 character "myfile". The program is as follows.

      #include<stdio. h>
      #include<string .h>

      int main()
      {
      FILE *fp;
      char *filename();

      if ((fp = fopen (filename(),"r" )) == NULL)

      {
      printf ("File could not be opened\n");
      // error_handler() ;
      }
      else
      {
      printf ("file opened");
      }
      }

      char *filename() /* return filename */

      { static char *filenm = ".............. ..........";

      do
      {
      printf ("Enter filename :");
      scanf ("%24s",&filenm );
      skipgarb();
      }
      while (strlen(filenm) == 0);
      return (filenm);
      }

      skipgarb() /* skip garbage corrupting scanf */

      {
      while (getchar() != '\n')
      {
      }
      }

      The error which I get is as follows. Help.

      [sagar@localhost ~]$ gcc fileopp4.c
      [sagar@localhost ~]$ ./a.out
      Enter filename :myfile
      Segmentation fault
      [sagar@localhost ~]$

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Try using gets instead of scanf.

        Savage

        Comment

        • sagarbsa
          New Member
          • Sep 2007
          • 5

          #5
          No Better Luck. Same Error.

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by sagarbsa
            No Better Luck. Same Error.
            It looks like that this happens because filename is a pointer,change it to stack array and it will work.

            Savage

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by Savage
              It looks like that this happens because filename is a pointer,change it to stack array and it will work.

              Savage
              You can get it to work with pointer also,but you must first initialize pointer to a valid address.

              Savage

              Comment

              • sagarbsa
                New Member
                • Sep 2007
                • 5

                #8
                Hii thanks for your reply. I modified the program for better debugging and is as follows.
                #include<stdio. h>
                #include<string .h>
                #include<stdio. h>
                #include<string .h>

                int main()
                {
                #include<stdio. h>
                #include<string .h>

                char *filename() /* return filename */

                {
                static char *filenm = ".............. ..........";
                printf("Entered the *file function\n");
                do
                {
                printf ("Enter filename :");
                scanf ("%24s",filenm) ;
                printf("Took file name\n");
                skipgarb();
                }
                while (strlen(filenm) == 0);
                return (filenm);
                }

                skipgarb() /* Skip input garbage corrupting scanf */
                {
                while (getchar() != '\n');
                printf("Finishe d skipgarb func");
                }

                int main()
                {
                FILE *fp;
                char *filename();

                if ((fp = fopen (filename(),"r" )) == NULL)

                {
                printf ("File could not be opened\n");
                }
                else
                {
                printf ("File opened\n");
                fclose (fp);
                printf("File closed sucessfully");
                }

                }

                Then when I ran the program the result is as follows.So the error is in the function where we enter the filename as it doent print the line "took file name". Give your suggestions. Tried gets also.

                [sagar@localhost cprogfiles]$ ./a.out
                Entered the *file function
                Enter filename :myfile
                Segmentation fault

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Read my previous two posts.

                  Savage

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by sagarbsa
                    static char *filenm = ".............. ..........";
                    Have you considered that filenm is a literal and that makes it const??? You cannot change the value of a literal.

                    Just create an array:
                    [code=c]
                    static char filenm[] = ".............. ..........";
                    [/code]

                    and things will start working.

                    Comment

                    • dharmarajat
                      New Member
                      • Sep 2008
                      • 2

                      #11
                      Originally posted by weaknessforcats
                      Have you considered that filenm is a literal and that makes it const??? You cannot change the value of a literal.

                      Just create an array:
                      [code=c]
                      static char filenm[] = ".............. ..........";
                      [/code]

                      and things will start working.
                      (or)

                      just allocate memmory to get the input file name and point the poiter to the memmory, as

                      static char *filenm;
                      filenm = (char *)malloc(24);
                      scanf ("%24s",filenm) ;

                      it works...

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by dharmarajat
                        (or)

                        just allocate memmory to get the input file name and point the poiter to the memmory, as

                        static char *filenm;
                        filenm = (char *)malloc(24);
                        scanf ("%24s",filenm) ;

                        it works...
                        *ahem* this thread has been dead for about a year. No need to resurrect it.

                        kind regards,

                        Jos

                        Comment

                        Working...