-1

I am writing a program to store information from an input file and print out information as selected by the user. I have not gotten to the use selection part yet but I am running into a segmentation fault right off the bat. I know that this means that I am trying to access places in memory that either don't exist or that I cannot access.

I am unsure of what I am doing wrong. I am trying to store the information from the input file into my structure.

The input file is in this format

3
5 Name Name 10 56789
7 Name Name 7 67894
8 Name Name 10 89375

I have tried to access the structures directly as emp[1].id etc instead of emp[i].id and such. This also did not work.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// structures
struct emp
{
    int id;
    char firstname[10];
    char lastname[10];
    int department;
    float salary;
} emp[10];


// function prototypes
// nothing here yet

int main(int argc, char *argv[])
{

int i = 0;
int choice;

if(argc != 2){

printf("Usage: %s input.txt\n", argv[0]);
exit(EXIT_FAILURE);
}

FILE* inputFile;


inputFile = fopen("input.txt", "r");

    if(inputFile == NULL){
            printf("Error opening %s\n", argv[1]);
            exit(EXIT_FAILURE);
    }
// file is open now

// loop to save information from file into structure

 int num;

    fscanf(inputFile, "%d", &num);


            for(i = 0; i < num; i++){
    fscanf(inputFile, "%d", emp[i].id);
    fscanf(inputFile, "%s", emp[i].firstname);
    fscanf(inputFile, "%s", emp[i].lastname);
    fscanf(inputFile, "%d", emp[i].department);
    fscanf(inputFile, "%f", emp[i].salary);

}


    printf("\n");
    printf("Welcome to the Employee Database!\n");
    printf("---------------------------------\n");
    printf("Choose an option:\n");
    printf("1:   Print empid\n");
    printf("2:   Print ALL employees\n");
    printf("3:   Show ALL employees in department\n");
    printf("-1:  QUIT\n");
    scanf("%d", &choice);

// I have not set up the functions to perform the selection options yet 
return 0;
}

This is the output that I am receiving.

c803@cs2:~A5$ gcc A5.c
c803@cs2:~A5$ ./a.out input.txt
Segmentation fault
6
  • What does the debugger say about the crash?
    – melpomene
    Jun 16, 2019 at 17:00
  • 2
    Always enable compiler warnings: gcc -Wall -Wextra -pedantic -O2 A5.c
    – melpomene
    Jun 16, 2019 at 17:00
  • maybe numis larger than 10? Jun 16, 2019 at 17:06
  • 2
    A5.c:60:29: warning: format '%d' expects argument of type 'int *', but argument 3 has type 'int' [-Wformat=] fscanf(inputFile, "%d", emp[1].id); ^ A5.c:63:29: warning: format '%d' expects argument of type 'int *', but argument 3 has type 'int' [-Wformat=] fscanf(inputFile, "%d", emp[1].department); ^ I will work to see if I can solve these issues now. Thank you. Jun 16, 2019 at 17:07
  • SERIOUSLY. I just forgot the &... THANK YOU. Jun 16, 2019 at 17:12

1 Answer 1

2

Here fscanf takes memory address of the variables to store the read data into, just like scanf(). You need to put '&' in front of emp[i].id and all other data memebers except character arrays as array name itself gives address of first array members of the array. So the code should be::

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// structures
struct emp
{
    int id;
    char firstname[10];
    char lastname[10];
    int department;
    float salary;
} emp[10];


// function prototypes
// nothing here yet

int main(int argc, char *argv[])
{

int i = 0;
int choice;

if(argc != 2){

printf("Usage: %s input.txt\n", argv[0]);
exit(EXIT_FAILURE);
}

FILE* inputFile;


inputFile = fopen("input.txt", "r");

    if(inputFile == NULL){
            printf("Error opening %s\n", argv[1]);
            exit(EXIT_FAILURE);
    }
// file is open now

// loop to save information from file into structure

 int num;

    fscanf(inputFile, "%d", &num);


            for(i = 0; i < num; i++){
    fscanf(inputFile, "%d", &emp[i].id);
    fscanf(inputFile, "%s", emp[i].firstname);
    fscanf(inputFile, "%s", emp[i].lastname);
    fscanf(inputFile, "%d", &emp[i].department);
    fscanf(inputFile, "%f", &emp[i].salary);

}


    printf("\n");
    printf("Welcome to the Employee Database!\n");
    printf("---------------------------------\n");
    printf("Choose an option:\n");
    printf("1:   Print empid\n");
    printf("2:   Print ALL employees\n");
    printf("3:   Show ALL employees in department\n");
    printf("-1:  QUIT\n");
    scanf("%d", &choice);

// I have not set up the functions to perform the selection options yet 
return 0;
}
1
  • YEP. Something silly and simple. Thank you so much! Jun 16, 2019 at 17:14

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.