Thread: Segmentation Fault Help

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    55

    Segmentation Fault Help

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    
    {
    
    FILE *ofp;
    
    ofp = fopen("Cards.dat", "r");
    
    if (ofp == NULL)
    {
     printf("Unable to open Cards.dat");
    }
    
    int numbers[10][2];
    int r, c, n;
    
    for (r = 0; r < 100; ++r)
       {
       for(c = 0; c < 2; ++c)
          {
          fscanf(ofp, "%d", &n);
          numbers[r][c] = n;
          }
       }
    
    printf("%d\n", numbers[0][0]);
    
    
    
    
    
    return 0;
    
    }
    I can't find why it's giving me a segmentation fault error

    Here's the file it's pulling from...
    Code:
    1 4
    11 3
    4 2
    0 0
    The printf statement at the end is just to check if it's storing into the numbers array correctly.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Impatient little cuss, ain't he?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Who me?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yeah... you started two identical threads less than 5 minutes apart....

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can't put 100 entries into an array of only 10 elements.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Wild guess, your open is failing and ofp is null.
    You should exit out if its null.

    Dylan

    Also you should move
    int numbers[10][2];
    int r, c, n;
    Before the open.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by dylan View Post
    Wild guess, your open is failing and ofp is null.
    You should exit out if its null.

    Dylan

    Also you should move

    Before the open.
    yep, not sure too but he should have used an else if/else..then he would know for sure..
    but I think the main problem is he went out of bounds...
    array[11][0]==seg fault
    You ended that sentence with a preposition...Bastard!

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    55

    Tater

    Quote Originally Posted by CommonTater View Post
    Yeah... you started two identical threads less than 5 minutes apart....
    Yes...I does it not tater...so? Join he is not.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >yep, not sure too but he should have used an else if/else..then he would know for sure..
    Or perhaps just placing a return inside the if

    I'm not really sure if OP has really got the point on SEG FAULT.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    55

    Another Segmentation Fault

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    
    {
    
    FILE *ofp;
    FILE *ifp;
    
    int n, i, sum, input[100];
    i = 0;
    char numbers[][15] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    char suite[][15] = {"Clubs", "Diamonds", "Hearts", "Spades"};
    
    ofp = fopen("Result.dat", "w");
    
    ifp = fopen("Cards.dat", "r");
    
    if (ifp == NULL)
    {
     printf("Unable to open Cards.dat");
    }
    
    fscanf(ifp, "%d", &n);
    
    while (n != 0)
    {
     input[i] = n - 1;
     ++i;
     fscanf(ifp, "%d", &n);
    }
    
    sum = i;
    
    for (i = 0; i < sum; i = i + 2)
     {
      fprintf(ofp, "%s of %s\n", numbers[input[i]], suite[input[i - 1]]);
     }
    
    
    
    
    
    return 0;
    
    }
    Not sure where it's getting this segmentation fault from...Any clues?

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    char numbers[13][] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    char suite[4][] = {"Clubs", "Diamonds", "Hearts", "Spades"};
    Do some reading... A Tutorial on Pointers and Arrays in C ... it may help.

    Code:
    for (i = 0; i < sum; i = i + 2)
     {
      fprintf(ofp, "%s of %s\n", numbers[input[i]], suite[input[i - 1]]);
     }
    What happens in the marked section above when i = 0?

    You are also opening files that you don't close...

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Quote Originally Posted by CommonTater View Post
    Code:
    char numbers[13][] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    char suite[4][] = {"Clubs", "Diamonds", "Hearts", "Spades"};
    Do some reading... A Tutorial on Pointers and Arrays in C ... it may help.

    Code:
    for (i = 0; i < sum; i = i + 2)
     {
      fprintf(ofp, "%s of %s\n", numbers[input[i]], suite[input[i - 1]]);
     }
    What happens in the marked section above when i = 0?

    You are also opening files that you don't close...
    Thanks Tater for the help! That tutorial is awesome!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd Segmentation Fault
    By JohnnyAppleseed in forum C Programming
    Replies: 7
    Last Post: 08-24-2010, 12:01 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

Tags for this Thread