Willytheboy
Willytheboy

Reputation: 29

C fscanf returning null

I have an issue with my homework where part of it requires scanning and reading some specific input. The related C code is:

typedef struct Train {
    int train_number;
    int priority;   // 0 is low priority, 1 is high priority
    int direction;  // 0 is Westbound, 1 is Eastbound
    float loading_time;
    float crossing_time;
    int loaded;
} Train;

#define MAX_TRAIN_COUNT 777
Train station[MAX_TRAIN_COUNT];

int main(int argc, char* argv[]) {
    //read the input file
    FILE *input;
    char c;
    int train_count = 0;
    input = fopen(argv[1], "r");

    while((c = getc(input)) != EOF) {
        if(c == '\n')
            train_count++;
    }

    int i;
    for (i = 0; i < train_count; i++) {
        char dir;
        int load, cross;
        fscanf(input, "%c %d %d\n", &dir, &load, &cross);
        printf("%c %d %d\n", dir, load, cross);
    }
    fclose(input);

The input is 3 rows consisting of one char and two integers separated by spaces.

e 10 6
W 6 7
E 3 10

The output I get is:

 4195728 0 

Where there is a space before the 4195728. I cannot seem to find a solution to fix this.

What seems to be the problem?

Upvotes: 0

Views: 728

Answers (1)

chux
chux

Reputation: 154592

Code fails to rewind the file before reading the data a 2nd time. Lack of checking input function success contributed to not exposing the problem. @WhozCraig.

int i;
rewind(input); // add
for (i = 0; i < train_count; i++) {

  // fscanf(input, "%c %d %d\n", &dir, &load, &cross);
  if (fscanf(input, "%c %d %d\n", &dir, &load, &cross) != 3) Handle_Error();

A better approach would handle reading the lines of the data file in a helper function.

Using fgets() is much better at handling troublesome input than fscanf(). @unwind

int ReadTrainData(Train *data, FILE *stream) {
  char buffer[200];
  if (fgets(buffer, sizeof buffer, stream) == NULL) {
    return EOF;
  }
  // Various ways to parse data.
  // This one look for a completed scan and checks that `n` was changed.
  int n = 0;
  //              v-- add space to consume optional leading white-space (if desired)
  sscanf(buffer, " %c %d %d %n", &data.direction, &data.loading_time,
      &data.crossing_time, &n);
  if (n == 0) return 0; // data incomplete
  if (buffer[n]) return 0; // extra junk in line

  // maybe valid data
  if (strchr("NSEWnsew", &data.direction) == NULL) return 0;
  if (data.loading_time < 0 || data.loading_time > TRAIN_TIME_MAX) return 0;
  // add other validation tests as needed

  return 1;
}

Sample usage

size_t train_count = 0;
if (input) {
  Train data = {0};
  while(ReadTrainData(&data, input) == 1) {
    train_count++;
    printf("%c %d %d\n", data.direction, data.loading_time, data.crossing_time);
  }
  fclose(input);
}

Upvotes: 1

Related Questions