Beginner programmer with segmentation fault.. HELP PLEASE!

This is a discussion on Beginner programmer with segmentation fault.. HELP PLEASE! within the C Programming forums, part of the General Programming Boards category; There is a segmentation fault somewhere near the beginning of the program but I cant figure out whats causing it.. ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Beginner programmer with segmentation fault.. HELP PLEASE!

    There is a segmentation fault somewhere near the beginning of the program but I cant figure out whats causing it.. I think its happening in the 'Openfile' function..

    The programs supposed to read info from file line by line and sort it into structures.. example info: smart:kid:12345:12:12:12:12:A+

    Its a rough draft but compiles I just need help with the segmentation error.
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct
    {
    char last[30];
    char first[30];
    int id;
    int scores[4];
    char grade[5];
    float average;
    char fullname[60];
    } stuRec;
    
    
    bool OpenFile(char *argv[], FILE* *record);
    void ReadData(stuRec Record[], FILE* record, int* numofstu);
    void PrintMenu(int numofstu);
    void SortName(stuRec Record[], int numofstu);
    void SortId(stuRec Record[], int numofstu);
    void SearchName(stuRec Record[], int numofstu);
    void SearchId(stuRec Record[], int numofstu);
    void PrintReport(stuRec Record[], int numofstu);
    
    
    int main(int argc, char *argv[])
    {
    int option, numofstu;
    stuRec Record[80];
    char nameSearch[20];
    char idSearch[6];
    FILE* record;
    
    
    if(argc != 2)
    {
    printf("No filename entered."); 
    return 0;
    }
    if(OpenFile(argv, &record))
    return 0;
    ReadData(Record, record, &numofstu);
    
    do
    {
    PrintMenu(numofstu);
    scanf("%d", &option);
    switch (option)
    {
    case 1: SortName(Record, numofstu);
    printf("The students have been sorted by the last name field");
    break;
    
    case 2: SortId(Record, numofstu);
    printf("The students have been sorted using the student id field");
    break;
    
    case 3: PrintReport(Record, numofstu);
    break;
    
    case 4: SearchName(Record, numofstu);
    break;
    
    case 5: SearchId(Record, numofstu);
    break;
    
    case 6: break;
    
    default: printf("Invalid input. Please try again\n");
    return 0;
    }
    
    } while (option != 6);
    return 0;
    }
    
    
    bool OpenFile(char *argv[], FILE* *record)
    {
    *record = fopen(argv[1], "r");
    if(*record == NULL)
    {
    printf("Error opening %s, please try again.\n", argv[1]); 
    return true;
    }
    else
    return 0;
    }
    
    
    void ReadData(stuRec Record[], FILE* record, int* numofstu)
    {
    char data[60];
    int i;
    
    for(i = 0, *numofstu = 0; i < 80; i++, *numofstu++)
    {
    fgets(data, sizeof(data), record);
    if(8 != (sscanf(data, "%[^:]%*c%[^:]%*c%d%*c%d%*c%d%*c%d%*c%d%*c%s", Record[i].last, Record[i].first, Record[i].id, Record[i].scores[0], Record[i].scores[1], Record[i].scores[2], Record[i].grade)));
    {
    printf("Invalid data: %s", data);
    i--;
    *numofstu--;
    continue;
    }
    if(sizeof(Record[i].last) > 10 || sizeof(Record[i].first) > 10 || Record[i].id < 30000 || Record[i].id > 79999 || Record[i].scores[0] < 0 || Record[i].scores[0] > 125 || Record[i].scores[1] < 0 || Record[i].scores[1] > 125 || Record[i].scores[2] < 0 || Record[i].scores[2] > 125 || Record[i].scores[3] < 0 || Record[i].scores[3] > 125)
    {
    printf("Invalid record: %s", data);
    i--;
    *numofstu--;
    continue;
    }
    Record[i].average = ((Record[i].scores[0] + Record[i].scores[1] + Record[i].scores[2] + Record[i].scores[3])/4);
    }
    for(i = 0; i < *numofstu; i++)
    {
    strcpy(Record[i].fullname, Record[i].last);
    sprintf(Record[i].fullname, ", ");
    strcat(Record[i].fullname, Record[i].first);
    }
    }
    
    void PrintMenu(int numofstu)
    {
    printf("There are %d records available.\n", numofstu);
    printf("The actions available are: \n");
    printf(" 1. Sort the records by the lastname field.\n");
    printf(" 2. Sort the records by the Student ID field.\n");
    printf(" 3. Print a report of the records.\n");
    printf(" 4. Search for a record by Student last name and print the record.\n");
    printf(" 5. Search for a record by Student ID and print the record.\n");
    printf(" 6. Quit\n");
    printf("Enter the number corresponding to the action that you want to perform:");
    }
    
    void SortName(stuRec Record[], int numofstu)
    {
    bool located;
    stuRec temp;
    stuRec* pcurrent;
    stuRec* pwalker;
    stuRec* plast;
    
    for(pcurrent = Record + 1, plast = Record + numofstu; pcurrent <= plast; pcurrent++)
    {
    located = false;
    temp = *pcurrent;
    
    for(pwalker = pcurrent - 1; pwalker >= Record && !located; )
    if(strcmp(temp.last, pwalker->last) < 0)
    {
    *(pwalker +1) = *pwalker;
    pwalker--;
    }
    else
    located = true;
    *(pwalker +1) = temp;
    }
    }
    
    void SortId(stuRec Record[], int numofstu)
    {
    int walk, temp, current;
    bool located;
    
    for(current = 1; current <= numofstu; current++)
    {
    located = false;
    temp = Record[current].id;
    for(walk = current - 1; walk>= 0 && ! located; )
    if(temp < Record[walk].id)
    {
    Record[walk + 1].id = Record[walk].id;
    walk--;
    }
    else
    located = true;
    Record[walk +1].id = temp;
    }
    
    }
    
    void SearchName(stuRec Record[], int numofstu)
    {
    int i, j;
    char name[20];
    printf("\nPlease enter the student's last name: ");
    scanf("%s", name);
    stuRec Temp[80];
    
    for(i = 0, j = 0; i < numofstu; i++)
    if(0 == strncmp(Record[i].last, name, strlen(name)))
    {
    Temp[i] = Record[i];
    j++;
    }
    if(j >= 1)
    {
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    printf("| Student Name | ID | Test 1 | Test 2 | Proj 1 | Proj 2 | Average | Grade |");
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    }
    
    for(i = 0; i < j; i++)
    printf("+%24s| %d | %3d | %3d | %3d | %3d | %3.2f | %s |", Temp[i].fullname, Temp[i].id, Temp[i].scores[0], Temp[i].scores[1], Temp[i].scores[2], Temp[i].scores[3], Temp[i].average, Temp[i].grade);
    
    if(j >= 1)
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    }
    
    void SearchId(stuRec Record[], int numofstu)
    {
    int i, j;
    int id;
    printf("\nPlease enter the student's ID: ");
    scanf("%d", id);
    stuRec Temp[80];
    
    for(i = 0, j = 0; i < numofstu; i++)
    if(id == Record[i].id)
    {
    Temp[i] = Record[i];
    j++;
    }
    if(j >=1)
    {
    
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    printf("| Student Name | ID | Test 1 | Test 2 | Proj 1 | Proj 2 | Average | Grade |");
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    }
    
    for(i = 0; i < j; i++)
    printf("+%24s| %d | %3d | %3d | %3d | %3d | %3.2f | %s |", Temp[i].fullname, Temp[i].id, Temp[i].scores[0], Temp[i].scores[1], Temp[i].scores[2], Temp[i].scores[3], Temp[i].average, Temp[i].grade);
    
    if(j >= 1)
    
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    }
    
    void PrintReport(stuRec Record[], int numofstu)
    {
    int i;
    
    
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    printf("| Student Name | ID | Test 1 | Test 2 | Proj 1 | Proj 2 | Average | Grade |");
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    for(i = 0; i < numofstu; i++)
    {
    printf("+%24s| %d | %3d | %3d | %3d | %3d | %3.2f | %s |", Record[i].fullname, Record[i].id, Record[i].scores[0], Record[i].scores[1], Record[i].scores[2], Record[i].scores[3], Record[i].average, Record[i].grade);
    }
    
    printf("+------------------------+-------+--------+--------+--------+--------+---------+-------+");
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Remember that the second argument you pass to scanf must be the address of the variable in which you wish scanf to store what it reads. For example, instead of passing an integer variable:
    Code:
    scanf("%d", id);
    You'd want to pass a pointer to the integer variable:
    Code:
    scanf("%d", &id);
    Kevin
    Last edited by kmess; 04-22-2011 at 06:23 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Yea thanks I overlooked that. Wish I could get the program to run though so I could debug further..

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    In your ReadData() function, it appears as though you have a stray ';' after your if statement...

    Kevin

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by ericcsmygod View Post
    Wish I could get the program to run though so I could debug further..
    How far does it get? Can you tell?

    If you can't do real debugging (and step through it), maybe adding some "debug" printfs would help.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by mike65535 View Post
    How far does it get? Can you tell?

    If you can't do real debugging (and step through it), maybe adding some "debug" printfs would help.
    I did. it happens at the end of the function Openfiles.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    bool OpenFile(char *argv[], FILE* *record)
    {
    *record = fopen(argv[1], "r");
    if(*record == NULL)
    {
    Lose the asteriscs ... it's just record... not *record.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    I'm just wondering who you copied this code off?

    > if(8 != (sscanf(data, "%[^:]%*c%[^:]%*c%d%*c%d%*c%d%*c%d%*c%d%*c%s"
    Because someone who can come up with syntax like that should really be able to figure out their own trivial segfault errors all by themselves.

    Or is it a case of "Here is some buggy code, go fix it" homework from your tutor?
    Either way, I'm doubtful that it is all YOUR code.


    It might help if your code wasn't formatted like dog-food to begin with. Where the hell is the indentation?
    SourceForge.net: Indentation - cpwiki

    Before you try debugging it, how about you compile it with some decent flags, and FIX all the warnings and errors.
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2  bar.c
    bar.c: In function ‘main’:
    bar.c:34: warning: unused variable ‘idSearch’
    bar.c:33: warning: unused variable ‘nameSearch’
    bar.c:50: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    bar.c: In function ‘ReadData’:
    bar.c:99: warning: value computed is not used
    bar.c:102: warning: format ‘%d’ expects type ‘int *’, but argument 5 has type ‘int’
    bar.c:102: warning: format ‘%d’ expects type ‘int *’, but argument 6 has type ‘int’
    bar.c:102: warning: format ‘%d’ expects type ‘int *’, but argument 7 has type ‘int’
    bar.c:102: warning: format ‘%d’ expects type ‘int *’, but argument 8 has type ‘int’
    bar.c:102: warning: format ‘%d’ expects type ‘int *’, but argument 9 has type ‘char *’
    bar.c:102: warning: too few arguments for format
    bar.c:102: warning: suggest braces around empty body in an ‘if’ statement
    bar.c:106: warning: value computed is not used
    bar.c:113: warning: value computed is not used
    bar.c:101: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result
    bar.c: In function ‘SearchName’:
    bar.c:192: warning: ISO C90 forbids mixed declarations and code
    bar.c:191: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    bar.c: In function ‘SearchId’:
    bar.c:219: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    bar.c:220: warning: ISO C90 forbids mixed declarations and code
    bar.c:219: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    bar.c:219: warning: ‘id’ is used uninitialized in this function
    See all those "%d" expects messages - they're segfaults waiting to happen.

    Edit:
    Of course - another selfish timewaster
    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. Help! Beginner Programmer....
    By Shodai100 in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2010, 02:54 AM
  2. Beginner programmer
    By Kool4School in forum C Programming
    Replies: 16
    Last Post: 11-24-2008, 12:16 PM
  3. [C++] Segmentation Fault {Novice C++ Programmer}
    By INFERNO2K in forum C++ Programming
    Replies: 24
    Last Post: 06-08-2005, 07:44 PM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 07:15 AM

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