-1

I've got a text file formatted like this:

100 0 10 1
101 6 10 1
102 8 4 1
103 12 20 1
104 19 15 1
105 30 5 1
106 35 10 1

I need to put these numbers into the arrays pID[], arrival[], bursts[], and priority[], respectively. C is not my strongest language, so I'm having some trouble doing this.

Here's my current code:

void readFile(int n, int pID[], int arrival[], int bursts[], int priority[]){
FILE *file;
int i = 0;
file = fopen("Process.txt", "r");

//File format is pID, arrival, bursts, and priority
if (file){
    while (!feof(file)){
        pID[i] = fscanf(file, "%d ", &i);
        arrival[i] = fscanf(file, "%d ", &i);
        bursts[i] = fscanf(file, "%d ", &i);
        priority[i] = fscanf(file, "%d ", &i);
    }
    fclose(file);
}

Thanks for any help!

  • [why-is-while-feof-file-always-wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). And "I'm having some trouble doing this" is not really **specific*. See [ask]. – too honest for this site Mar 23 '16 at 19:56
  • `fscanf` returns *the number of items successfully scanned*, not the scanned value. `if(fscanf(file, "%d", &pID[i]) != 1) exit(1);` – Weather Vane Mar 23 '16 at 19:58
  • Sorry, the values that are put into the arrays are no where near what is from the file. Thanks for the article. If !feof(file) is incorrect, what is the correct way to loop through each line of the file? – Christian Gonzales Mar 23 '16 at 19:59
  • Please read the post – KevinDTimm Mar 23 '16 at 20:31
  • Note that for interactive inputs, a trailing blank in a format string is a disaster; the `scanf()` operation won't return until the user types a non-blank (tab, newline) character after the end of the relevant data. That means they have to predict what you're going to request next — not good. – Jonathan Leffler Mar 23 '16 at 22:13

1 Answers1

1

You are using feof and fscanf in the wrong way. I suggest you read one line from file at a time, checking it was read, and then scanning the values from the buffer, also checking that the array index is still ok, and the correct number of fields were scanned.

void readFile(int n, int pID[], int arrival[], int bursts[], int priority[]) {
    FILE *file;
    int i = 0;
    char buffer[100];
    file = fopen("Process.txt", "r");
    if (file){
        while (i < n && fgets(buffer, sizeof buffer, file) != NULL) {
            if(sscanf(buffer, "%d%d%d%d", &pID[i], &arrival[i], &bursts[i], &priority[i]) != 4) {
                exit(1);                // or recovery strategy
            }
            i++;
        }
        fclose(file);
    }
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Thank you so much! This solved my problem, everything is being read in how it should be. – Christian Gonzales Mar 23 '16 at 20:20
  • So the sscanf line is checking to make sure there are four values in each line of the file, while also putting the respective values in the arrays? Sorry I keep asking so many questions, I'm just trying to fully understand how this solved my problem. – Christian Gonzales Mar 23 '16 at 20:24
  • Yes, that is correct. It is always a good idea to check whatever you can check: assume nothing for robust code, especially when the input source may not be within your control. Although note: this would simply ignore any other data in that text line if there were more than 4 numbers, but it *would* trap anything that wasn't a number. – Weather Vane Mar 23 '16 at 20:55
  • For ditching `fscanf()` and embracing `fgets()` when data is per _line_ --> upvote. – chux - Reinstate Monica Mar 23 '16 at 23:26