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; I have to write a program for my class and I am having a bit of trouble with the file ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    16

    Need help fixing segmentation fault

    I have to write a program for my class and I am having a bit of trouble with the file pointer.
    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);
    }
    This is what I have right now. It points to a file that has 3 columns of numbers/letters like this

    b A 1
    b B 4
    B b 1
    B C 2
    B C 3
    W A 3
    w A 1
    W B 3
    W B 2

    What I'm trying to do is scan those into arrays and later print them. The first column is stone_color the second is column and the third should be row. Everything compiles correctly and the file is valid and in the same directory however when I run it I get a segmentation fault. Any ideas?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Why are you telling fscanf you are reading in strings when you only want individual characters? Additionally, there is no need for three seperate fscanfs here, just use your format string to read in the 3 columns at one time.
    Last edited by AndrewHunter; 09-11-2011 at 02:57 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Quote Originally Posted by AndrewHunter View Post
    Why are you telling fscanf you are reading in strings when you only want individual characters? Additionally, there is no need for three seperate fscanfs here, just use your format string to read in the 3 columns at one time.
    Ok switched the %s's to %c but I'm still getting segmentation fault. How would I write it so that it reads 3 columns at a time?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Wrong...
    Code:
    while( fscanf(fp, "%s", stone_color[i]) != EOF) {
    fscanf(fp, "%s", column[i]);
    fscanf(fp, "%d", row[i]);
    Less wrong...
    Code:
    while (scanf(" %c %c %d", &stone_color[i], &column[i],&row[i++]) > 2);
    Look up scanf() in your C library documentation (yes, really, look it up!)...

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    And there's a difference between assignment and testing for equality.
    Code:
    i == 0;  /* surely you meant i = 0 */

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    Wrong...
    Code:
    while( fscanf(fp, "%s", stone_color[i]) != EOF) {
    fscanf(fp, "%s", column[i]);
    fscanf(fp, "%d", row[i]);
    Less wrong...
    Code:
    while (scanf(" %c %c %d", &stone_color[i], &column[i],&row[i++]) > 2);
    Look up scanf() in your C library documentation (yes, really, look it up!)...
    Can you tell me why I'd use scanf instead of fscanf and why the condition is >2?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    [QUOTE=Robbie;1051725]Can you tell me why I'd use scanf instead of fscanfMy bad... you should be using fscanf() to read a file...

    and why the condition is >2?
    Did you take my advice and look it up in your C library documentation?

    Hint: if you had A) you would not have had to ask the original question and B) you would know why > 2 ....

    Seriously, DO NOT become one of these idiots who won't open a help file to save their imortal souls... A programmers second most important skill --right behind problem analysis-- is "looking stuff up"!

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    [QUOTE=CommonTater;1051726]
    Quote Originally Posted by Robbie View Post
    Can you tell me why I'd use scanf instead of fscanfMy bad... you should be using fscanf() to read a file...



    Did you take my advice and look it up in your C library documentation?

    Hint: if you had A) you would not have had to ask the original question and B) you would know why > 2 ....

    Seriously, DO NOT become one of these idiots who won't open a help file to save their imortal souls... A programmers second most important skill --right behind problem analysis-- is "looking stuff up"!
    Actually I did go back and read a little section on scanf the first time. Nothing. After your last post I said to myself "Hey maybe this guy is on to something. Maybe I should check the book again." At that point I cracked open the book again, read a decent sized section on scanf and fscanf in an input/output chapter and still...no answer. I've been working on debugging this one thing for hours. I've done countless google searches and read countless tutorials. I just want to figure out how to fix it and move on with my project.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    Somehow, I'm also a little confused!! Why scanf() instead of fscanf(), when his source of input is a file and not standard input stream(assuming its not redirected)???

    To be more precise, may be he can even use "==3" rather than ">2".

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Robbie View Post
    Actually I did go back and read a little section on scanf the first time. Nothing. After your last post I said to myself "Hey maybe this guy is on to something. Maybe I should check the book again." At that point I cracked open the book again, read a decent sized section on scanf and fscanf in an input/output chapter and still...no answer. I've been working on debugging this one thing for hours. I've done countless google searches and read countless tutorials. I just want to figure out how to fix it and move on with my project.
    What book? Your course textbook? K&R? ....

    No, I specifically said "C Library Documentation" ... Textbooks only give you enough information to get the general idea. If you want to know what's really going on, your compiler's documentation is where the REAL information is.

    For example... http://sca.uwaterloo.ca/coldfire/gcc...s/libc_78.html

    (I do hope you aren't alergic to following instructions... it's a tragic disorder for a programmer to have!)
    Last edited by CommonTater; 09-11-2011 at 04:30 PM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Avenger625 View Post
    Somehow, I'm also a little confused!! Why scanf() instead of fscanf(), when his source of input is a file and not standard input stream(assuming its not redirected)???

    To be more precise, may be he can even use "==3" rather than ">2".
    He could. But it's not more precise... merely an alternative.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    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!!

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    [QUOTE=CommonTater;1051741]
    Quote Originally Posted by Robbie View Post

    What book? Your course textbook? K&R? .... no, I specifically said "C Library Documentation" ... Textbooks only give you enough information to get the general idea. If you want to know what's really going on, your compiler's documentation is where the REAL information is.

    (I do hope you aren't alergic to following instructions... it's a tragic disorder for a programmer to have!)
    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.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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!!
    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.

    And "u" is not a word, it's a letter from the alphabet... If you're addressing a human being use "you".

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    [QUOTE=Robbie;1051747]
    Quote Originally Posted by CommonTater 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.
    That is a great attitude to have, I am sure everyone will be willing to help you now.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Page 1 of 4 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, 04:57 PM
  2. Segmentation fault
    By Miffeltoffel in forum C Programming
    Replies: 8
    Last Post: 10-21-2010, 04:55 AM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  4. Segmentation Fault
    By Haos in forum C Programming
    Replies: 2
    Last Post: 09-18-2004, 09:00 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

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