Segmentation Fault Help

This is a discussion on Segmentation Fault Help within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<stdlib.h> int main() { FILE *ofp; ofp = fopen("Cards.dat", "r"); if (ofp == NULL) { printf("Unable to open ...

  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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your r had better not go over 10.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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