Julian Hernandez
Julian Hernandez

Reputation: 269

Code that isn't scanning everything correctly

I'm supposed get get a file and scan in information. When I try printing it out, most of information I scanned becomes missing or chopped off. here's my code:

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

typedef struct profile {

    char gender;
    float soc;
    char name[20];
    char name2[20];
    int age;
    char job[20];
    float income;
    char M_hob[20];
    char m_hob[20];
    int height;
    int weight;
    char relig[20];
} PROFILE;

int main(void)
{

    int i, count;
    char file[30], ssn[10];
    FILE *fin;
    PROFILE members[50];

    printf("SSN: ");
    scanf("%s", ssn);
    printf("Name of file of records: ");
    scanf("%s", file);

    fin = fopen(file, "r");

    if ((fin == NULL)) {
        printf("Can't Open File");
        exit(1);
    }

    i = 0;
    while (fscanf(fin, "%c", &members[i].gender) != EOF) {

        fscanf(fin, "%f", &members[i].soc);
        fscanf(fin, "%s", members[i].name);
        fscanf(fin, "%s", members[i].name2);
        strcat(members[i].name, members[i].name2);
        fscanf(fin, "%d", &members[i].age);
        fscanf(fin, "%s", members[i].job);
        fscanf(fin, "%f", &members[i].income);
        fscanf(fin, "%s", members[i].M_hob);
        fscanf(fin, "%s", members[i].m_hob);
        fscanf(fin, "%d", &members[i].height);
        fscanf(fin, "%d", &members[i].weight);
        fscanf(fin, "%s", members[i].relig);
        fscanf(fin, "%c", &members[i].space);

        i++;
    }
    fclose(fin);

    count = i;
    for (i = 0; i <= count; i++) {
        printf("%c\n", members[i].gender);
        printf("%.0f\n", members[i].soc);
        printf("%s\n", members[i].name);
        printf("%d\n", members[i].age);
        printf("%s\n", members[i].job);
        printf("%.0f\n", members[i].income);
        printf("%s\n", members[i].M_hob);
        printf("%s\n", members[i].m_hob);
        printf("%d\n", members[i].height);
        printf("%d\n", members[i].weight);
        printf("%s\n", members[i].relig);
    }

    return 0;
}

Two paragraphs of what I'm scanning in:

M
111223333
Rob Low
47
Actor
1000000
Dancing
Painting
63
165
Atheist

M
123456789
Bob Mitchell
77
Driver
25000
Baseball
Cooking
72
275
None

and part of what gets printed out:

M
111223336
Rob
47
Actor
1000000
Dancing
Painting
63
165
Atheist

0
M
0
Bob
0
Mitchell
77
0
0
Driver
2
5000
Baseball
72
275
0
ne

as you can see the first one gets scanned in perfectly but the rest are buggy.

Upvotes: 1

Views: 127

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753655

If you must use fscanf(), check every return value.

Worry about the %c at the front of the loop getting a newline or thereabouts on the second cycle.

You'd do better to read each line into a big string, then apply sscanf() to it. This at least avoids confusion over newlines. (I wouldn't touch scanf() or fscanf() with a bargepole because of the way they handle newlines - or not.)

Upvotes: 1

Related Questions