Segmentation Fault

This is a discussion on Segmentation Fault within the C Programming forums, part of the General Programming Boards category; Hey all, thanks for checking my topic I need help with this - Code: #include <stdio.h> #include <stdlib.h> mostrar_menu () ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    9

    Unhappy Segmentation Fault

    Hey all, thanks for checking my topic

    I need help with this -


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
        mostrar_menu ()
        {    
            char option;
            
            puts("\n\tAUTOMIBLES\n");
            puts("0 - Exit");
            puts("1 - Read TEXT file");
            
            printf("Choose an option: ");
            scanf("%c", &option);
                
                switch(option)
                    {
                        case '0': puts("Goodbye"); break;
                        case '1': {ler_ficheiro(); break;}
                        default : { puts("Wrong option!\n"); mostrar_menu();}
                    }
        }
        
    
    /* -------------------------------------------------*/
    
    
    
    int ler_ficheiro()
    {
      char s[30];
      FILE *fp;
     
        puts("Input file name: ");
        gets(s);
        gets(s);
    
        fp= fopen(s, "r");
            
            if (fp==NULL)
                printf("Reading the %s file was not possible \n", s);
            else
                {
                ler_fich_texto();
                }
                
    }
    
    /* -------------------------------------------------*/
    
    int  ler_fich_texto(const char *fp)
    {
      int parametros;
      int result;
      FILE *stream;
      char s;
      
      
      parametros = 0;
      fopen(fp, "r");
      if ( stream )
      {
        parametros = 0;
        while ( fgets(&s, 150, stream) )
        {
          ++parametros;
        }
        fclose(stream);
        printf(" %d parameters were read.\n", parametros);
      }
      else
      {
        printf("Error opening %s file!\n", fp);
        result = 0;
      }
      mostrar_menu ();
    }
    
    
    /* -------------------------------------------------*/
    
    main ()
    {
        int i=1;
        while(i==1)
        {
        mostrar_menu();
        i++;
        }
    }
    So basically I'm using CYGWIN to compile my program - and I get a segmentation fault (core dumped) error.

    What I want this to do is read a .txt (database) and tell me how many parameters are there in that file.

    NOTE: Only option 0 and 1 are supposed to be working, the others are just so I don't forget.

    The .txt I'm talking about is this one right here -
    click


    Any help would be nice, thanks!
    Last edited by Kalastrian; 01-05-2012 at 06:18 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Line 57 ... you need to make storage for the strings ... char s[150]; ... adjust as needed.
    Line 65 ... No & in front of the s parameter.

    By the way... all you're doing is line counting... I hope that's what you intended.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    Yes it is. On line 65 what do you mean - I should do while ( fgets(&s&, 150, stream) ) ?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,683
    Quote Originally Posted by Kalastrian View Post
    Yes it is. On line 65 what do you mean - I should do while ( fgets(&s&, 150, stream) ) ?
    fgets - C++ Reference

    I am thinking he wants you to use an character array (or char pointer) instead of a single char.

    If you use an char pointer; you need to allocate space for it!

    Tim S.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,565
    Once you make s a char array, like char s[150]; then you want to remove the & from line 65. & gives you the address of something. But when you use the name of an array by itself, without any [ ], C gives you the address of the first element, which is exactly what fgets wants, so it should look like:
    Code:
    char s[150];
    ...
    fgets(s, sizeof(s), stream);
    Note, I use sizeof(s) so that if you change the size of the buffer (array), you only need to change it in one place.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    Thanks for the replies.
    Also, I'm sorry but English isn't my native language, so... Sorry if I'm a little daft.

    I've made the changes you've recommended, made s a character array by s[150] and removed the & from the fgets.
    After compiling, I still get the segmentation fault.


    Sorry to ask, but is it possible I can get some more help?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,565
    You should compile with your warnings turned all the way up. Read your compiler documentation. Here's what I get when I compile your program:
    Code:
    $ gcc -Wall -g  menu.c   -o menu
    menu.c:7: warning: return type defaults to ‘int’
    menu.c: In function ‘mostrar_menu’:
    menu.c:22: warning: implicit declaration of function ‘ler_ficheiro’
    menu.c: In function ‘ler_ficheiro’:
    menu.c:51: warning: implicit declaration of function ‘ler_fich_texto’
    menu.c: At top level:
    menu.c:86: warning: return type defaults to ‘int’
    menu.c: In function ‘main’:
    menu.c:92: warning: control reaches end of non-void function
    menu.c: In function ‘ler_fich_texto’:
    menu.c:80: warning: control reaches end of non-void function
    menu.c: In function ‘ler_ficheiro’:
    menu.c:54: warning: control reaches end of non-void function
    menu.c: In function ‘mostrar_menu’:
    menu.c:30: warning: control reaches end of non-void function
    menu.c: In function ‘ler_fich_texto’:
    menu.c:68: warning: ‘stream’ is used uninitialized in this function
    /tmp/cc9EfHNG.o: In function `ler_ficheiro':
    /home/tyco/sandbox/cprogramming/menu.c:43: warning: the `gets' function is dangerous and should not be used.
    For the "implicit declaration" warnings, you need to declare a function before you use it. That means put the whole function above where you call it, or put a prototype at the top of the file.

    For the "return type defaults to 'int'" and "control reaches end of non-void function" errors:
    In C, if you leave off the return type for a function, it assumes you're going to return an int. If you don't want to return anything from your functions, then make the return type 'void'. main should be declared to explicitly return an int: int mainI(void), and you should put a return 0; as the last line.

    Don't use recursion to do your error checking. Use a loop:
    Code:
    do {
        prompt user
        read input
    } while (input is not valid);
    You never pass any parameters to your functions or return any useful information from them. Data doesn't magically move between functions, you have to pass it and return it explicitly. If you definitely don't want to pass data, you should make the parameter list void. Start by reading this link: Functions in C - Cprogramming.com, then Google around for some other function tutorials.

    As for your seg fault, you use stream uninitialized in ler_fich_texto. It contains garbage data. It's probably not zero, so the if check succeeds, but it also doesn't point to a valid file, so using it in fread is undefined behavior and probably the result of your crash.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,683
    You have the function mostrar_menu() calling itself; this is wrong.
    Remove it.

    Tim S.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    72
    on line 45 you're calling ler_fich_texto();
    but on line 52 we see it needs a char * argument.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault
    By deepseawolf in forum C Programming
    Replies: 18
    Last Post: 03-13-2011, 08:55 PM
  2. Segmentation Fault
    By dulipat in forum C Programming
    Replies: 3
    Last Post: 05-25-2010, 10:48 PM
  3. Segmentation Fault help!
    By jonktay in forum C Programming
    Replies: 11
    Last Post: 05-21-2010, 12:20 PM
  4. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21