Like Tree2Likes

Need help fixing segmentation fault

This is a discussion on Need help fixing segmentation fault within the C Programming forums, part of the General Programming Boards category; Originally Posted by Robbie Listen dawg....I popped a Vyvanse at around 4:00 P.M. yesterday thinking "Hell yeah gonna program the ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Robbie View Post
    Listen dawg....I popped a Vyvanse at around 4:00 P.M. yesterday thinking "Hell yeah gonna program the ........ out dis project." Sat down at my computer...read through the project, opened up putty and got to work. About 30 minutes in I go to compile and make sure everything is straight. "gcc boardtest.c" "Aw snap it compiled I'm da man!"-me "./a.out"
    "Segmentation Fault (core dumped)" "Aight den aint no thang I'll fixed this up right quick." No. Its 2:34 P.M. now...about 3 hours of sleep max...and I still can't fix this. I'm not down for scavenger hunts at this point ya feel me? Ya boi just wants da solution.
    Don't you dare pull that ghetto-blabber crap on me ...

    I AM giving you the solution... and in your case it is to look up the function calls before you try to use them... dead serious, my friend... that's how REAL programmers work. Had you started doing this at 4:00pm yesterday, you would have finished in a couple of hours and not had to be on here getting all indignant and rude with people who are volunteering their time to help you!

    Now stop wasting evryone's time and do some real work...
    Last edited by CommonTater; 09-11-2011 at 03:42 PM.

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    The first step is to work on the indentation of your programs. The compiler doesn't care, but we do - it really helps to figure out problems.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char stone_color[82];
      char column[82];
      int row[82];
      int i;
      int x;
    
      FILE *fp;
      fp = fopen("goboard1.txt", "r");
    
      if (fp == NULL) {
        printf(" Not a valid pointer! \n");
      }
      i == 0;
      while (fscanf(fp, "%s", stone_color[i]) != EOF) {
        fscanf(fp, "%s", column[i]);
        fscanf(fp, "%d", row[i]);
        i++;
      }
      return (0);
    }
    Next, use a compiler which is capable of diagnosing printf/scanf mistakes.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:18: warning: statement with no effect
    foo.c:19: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c:20: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c:21: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’
    foo.c:10: warning: unused variable ‘x’
    In your case, ALL the parameters you're passing to fscanf are of the wrong type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Quote Originally Posted by Avenger625 View Post
    Try,
    Code:
    while (fscanf(fp," %c %c %d", &stone_color[i], &column[i],&row[i++]) == 3);
    and
    if ( fp == NULL ) {
    printf(" Not a valid pointer! \n");
    exit(1);
    }
    If u still get error could u plz copy the exact error message!!
    Code:
     boardtest.c:20: warning: incompatible implicit declaration of built-in function 'exit'
    thats what happens when I compile it now.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And now you need to look up exit() in your C Library documentation...

    Get it?

  5. #20
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char stone_color[82];
      	char column[82];
      	int row[82];
      	int i,x;
     
    
      	FILE *fp;
      	fp = fopen("goboard1.txt", "r");
    
      	if (fp == NULL) {
        		printf(" Not a valid pointer! \n");
    		exit(1);
      	}
      	i = 0;
     	
    	while (fscanf(fp," %c %c%d", &stone_color[i], &column[i],&row[i]) == 3)
    		i++;
    		
      	return (0);
    
    	/*for(x=0;x<i;x++)
    		printf("%c  %c  %d\n",stone_color[x],column[x],row[x]);*/
    }
    It should give u what u want!!!

  6. #21
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    Quote Originally Posted by CommonTater View Post
    He wants to check the file record pointer well before he tries the first call to fscanf() or the program will crash before he even gets to it.
    Did 'you' see that i wrote an "and" over there?! See the code I posted, hope 'you' will understand what i meant.

    And I will be obliged if 'you' give me the answer[I checked the link 'you' posted, but still clueless]-
    Why scanf() instead of fscanf(), when his source of input is a file and not standard input stream(assuming its not redirected)???

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Avenger625 View Post

    And I will be obliged if 'you' give me the answer[I checked the link 'you' posted, but still clueless]-
    Why scanf() instead of fscanf(), when his source of input is a file and not standard input stream(assuming its not redirected)???
    He's already admitted that was a misread/flub.

  8. #23
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Quote Originally Posted by Avenger625 View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char stone_color[82];
      	char column[82];
      	int row[82];
      	int i,x;
     
    
      	FILE *fp;
      	fp = fopen("goboard1.txt", "r");
    
      	if (fp == NULL) {
        		printf(" Not a valid pointer! \n");
    		exit(1);
      	}
      	i = 0;
     	
    	while (fscanf(fp," %c %c%d", &stone_color[i], &column[i],&row[i]) == 3)
    		i++;
    		
      	return (0);
    
    	/*for(x=0;x<i;x++)
    		printf("%c  %c  %d\n",stone_color[x],column[x],row[x]);*/
    }
    It should give u what u want!!!
    Thank you so much!!
    Code:
    	while (fscanf(fp," %c %c%d", &stone_color[i], &column[i],&row[i]) == 3)
    and just for clarification and future use...why ==3? Where does the 3 come from?

  9. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Avenger625 View Post
    Why scanf() instead of fscanf(), when his source of input is a file and not standard input stream(assuming its not redirected)???
    Because many library documents lump scanf(), fscanf() and sscanf() together... Looking one up usually gets you the others too...

    It was a mistake... it happens sometimes.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Robbie View Post
    Thank you so much!!
    Code:
    	while (fscanf(fp," %c %c%d", &stone_color[i], &column[i],&row[i]) == 3)
    and just for clarification and future use...why ==3? Where does the 3 come from?
    THIS is what I've been hounding you to look up! What is the return value of fscanf() and why should you care about it?
    Now you're sitting here asking a question you could have answered HOURS ago with a simple look at a help file.

    FYI ... it's the number of successful conversions... When all goes right it converts 3 values... When it hits end of file it will return something other than 3 ... if there's a bad line in the file it will return something other than three... get it?


    AND...
    Code:
    	while (fscanf(fp," %c %c %d", &stone_color[i], &column[i],&row[i]) == 3)
    ... the space between the %c and %d is necessary according to the file format you provided in your first message.

    But, once again... had you taken 5 minutes and read the documentation, you would have known that.

    Stupid is as stupid does!
    Last edited by CommonTater; 09-11-2011 at 04:32 PM.

  11. #26
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    If u follow the fscanf() prototype u will see it returns an int. It returns the number of fields successfully converted and assigned. The value does not include fields that have been read, but not assigned. We r reading and trying to assign 3 value. So if it returns 3 then it means there was no error in reading and assigning the values. Its doing what we wanted it to do.
    Last edited by Avenger625; 09-11-2011 at 04:38 PM.

  12. #27
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    THIS is what I've been hounding you to look up! What is the return value of fscanf() and why should you care about it?
    Now you're sitting here asking a question you could have answered HOURS ago with a simple look at a help file.

    FYI ... it's the number of successful conversions... When all goes right it converts 3 values... When it hits end of file it will return something other than 3 ... if there's a bad line in the file it will return something other than three... get it?
    Nothing I read mentioned anything about this. Thanks for the help though!

  13. #28
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    Quote Originally Posted by CommonTater View Post
    Because many library documents lump scanf(), fscanf() and sscanf() together... Looking one up usually gets you the others too...

    It was a mistake... it happens sometimes.
    Quote Originally Posted by tabstop View Post
    He's already admitted that was a misread/flub.

    I'm sorry...I somehow skipped it. I never wanted to disect/critisize his mistake....I'm really sorry!!!

  14. #29
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Robbie View Post
    Nothing I read mentioned anything about this. Thanks for the help though!
    From the link I provided you ... ( Cygnus C Support Library, Full - scanf )

    Quote Originally Posted by University of Waterloo
    Returns
    scanf returns the number of input fields successfully scanned, converted and stored; the return value does not include scanned fields which were not stored.

    If scanf attempts to read at end-of-file, the return value is EOF.

    If no fields were stored, the return value is 0.
    Gee... lookie that!

  15. #30
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Thanks everybody for all your help! I really appreciate it!

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By tlman12 in forum C Programming
    Replies: 1
    Last Post: 10-29-2010, 03:57 PM
  2. Segmentation fault
    By Miffeltoffel in forum C Programming
    Replies: 8
    Last Post: 10-21-2010, 03:55 AM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  4. Segmentation Fault
    By Haos in forum C Programming
    Replies: 2
    Last Post: 09-18-2004, 08:00 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