Seg Fault Error O_o

This is a discussion on Seg Fault Error O_o within the C Programming forums, part of the General Programming Boards category; 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; ...

  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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    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, 04:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 09:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 04:24 PM

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