Thread: Seg Fault Error O_o

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

    Seg Fault Error O_o

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct data_struct
    {
    int id;
    char fname[25];
    char lname[25];
    int height;
    int age;
    char degree[30];
    int bday;
    float wage;
    };
    
    
    
    int main()
    
    {
    
    struct data_struct record[1000];
    
    FILE *fp;
    
    
    fp = fopen("input", "r");
    
    if(fp == NULL)
     {
      printf("error opening input");
      exit(1);
     }
    
    int N = 0;
     
    fscanf(fp, "%d, %c, %c, %d, %d, %c, %d, %f", record[N].id, record[N].fname[25], record[N].lname[25], record[N].height, record[N].age, record[N].degree, record[N].bday, record[N].wage);
    
    
     
    while( !feof(fp) )
     {
      for(N = 0; N<200; N++)
       {
        printf("ID: %d \n%c %c \nHeight: %d \nAge: %d \nDegree: %c \nBirthDate: %d        Wage: %f", record[N].id, record[N].fname[25], record[N].lname[25], record[N].height, record[N].age, record[N].degree, record[N].bday, record[N].wage);
       }
     }
    
    
    return 0;
    }
    The goal of this is to read from a file that looks like this with much more input in it...

    THIS IS AN EXAMPLE:
    Code:
    100
    John
    Doe
    73
    19
    Cs
    11051991
    7.71
    101
    George
    Washington
    75
    18
    cons
    05141992
    5.01
    In order from top to bottom it goes: ID Number, First Name, Last Name, Height (inches), Age, Degree, birthdate(dd/mm/yyyy), wage.

    When I run the file I get a seg fault and can't figure out what my problem is...

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You do not need to “remind” the compiler of the size of an array: it knows what it is. So if you have an array:
    Code:
    int array[50];
    The name of this object is “array”, not “array[50]”. In fact, array[50] tries to access a non-existent element of the array. When you have an array as above, this means you have 50 ints you can work with. The first int is array[0], the second is array[1], and the last is array[49]. But each of these is an int, not an array! Remember, the array is simply “array”.

    Before you work with arrays of structs, I would recommend going back to the basics of C's input/output. %c doesn't read strings, for example. Looping on feof() is usually wrong, but especially so when you're not actually reading input.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Quote Originally Posted by cas View Post
    You do not need to “remind” the compiler of the size of an array: it knows what it is. So if you have an array:
    Code:
    int array[50];
    The name of this object is “array”, not “array[50]”. In fact, array[50] tries to access a non-existent element of the array. When you have an array as above, this means you have 50 ints you can work with. The first int is array[0], the second is array[1], and the last is array[49]. But each of these is an int, not an array! Remember, the array is simply “array”.

    Before you work with arrays of structs, I would recommend going back to the basics of C's input/output. %c doesn't read strings, for example. Looping on feof() is usually wrong, but especially so when you're not actually reading input.
    Thanks for the help!!! I'll keep on coding...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM