0

I want to be able to read each row of this txt file using scanf() and assigning each column to an array.

    220
    01 01 5.00 1.50
    02 01 4.00 3.00
    02 02 3.00 4.00
    03 01 4.50 3.00
    03 01 7.50 2.50
    03 01 6.00 0.50
    04 01 2.00 3.00
    04 02 2.00 3.00
    05 01 1.50 3.00
    07 01 5.00 1.00
    09 01 1.50 6.00
    -1

so far I only have:

    #define MAXSIZE 100    

    int main(int argc, char *argv[]) {
        int i;
        int a, b;
        double c, d, e;
        int A[MAXSIZE], B[MAXSIZE];
        double C[MAXSIZE], D[MAXSIZE

        i = 0;
        scanf("%d %d %lf %lf", &a, &b, &c, &d);

        A[i] = a;
        B[i] = b;
        C[i] = b;
        D[i] = d;

        i+=1;
    }

Is there any way I can do this?

Axifive
  • 1,159
  • 2
  • 19
  • 31
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • 1
    Well and just for starters, don't do it with `scanf`. [Never use `scanf`](https://stackoverflow.com/questions/15664664/scanf-regex-c/15664816#15664816). (Use `strtok`, `strtod`, and `strtoul` instead. Pay close attention to the part of the `strtoul` manpage where it explains how to check for errors.) – zwol Apr 21 '16 at 00:02
  • 1
    If you add `];` to the end of `D[MAXIZE` and and add a for loop that should do it – Nevado Apr 21 '16 at 00:05
  • 1
    and `C[i] = b;` --> `C[i] = c;` and use while-loop and `./a.out < data.txt` – BLUEPIXY Apr 21 '16 at 00:07
  • 1
    open file with `fopen` , then use `fscanf` see [here](http://en.cppreference.com/w/c/io/fscanf) – norisknofun Apr 21 '16 at 00:07
  • 2
    @Opticgenius try `fgets` then `sscanf` – BLUEPIXY Apr 21 '16 at 00:19
  • 3
    If you want line-based input, do not use `fscanf()` or `scanf()`; they don't recognize line boundaries as important. Use `fgets()` and then `sscanf()`, as suggested by BLUEPIXY. How are you supposed to handle the lines at the start and end with one number each? What is their significance? You can recognize them easily enough: `int n_cols = sscanf(buffer, "%d %d %lf %lf", …); if (n_cols == 1) { …process special line… } else if (n_cols == 4) { …process normal line… } else { … process error or EOF… }`. One major advantage of `fgets()` plus `sscanf()` is you can print the line that had problems. – Jonathan Leffler Apr 21 '16 at 00:46
  • 1
    @JonathanLeffler Are you aware that numeric overflow during conversion in any of the `scanf` functions provokes undefined behavior? – zwol Apr 21 '16 at 13:16
  • 2
    @zwol: yes, no, maybe. Yes, every so often I re-remember that there is an issue there. No, I'm not going to recommend to beginners that they don't use the `scanf()` family of functions. As in this case, it is often better to use line-based input and the `sscanf()` rather than just `scanf()`. Maybe if I was worried about the UB, I would arrange to use a surrogate implementation of the `scanf()` family that didn't incur UB on out of range values (e.g. because it used the equivalent of the `strto*()` functions to convert values). – Jonathan Leffler Apr 21 '16 at 13:40

1 Answers1

1

Be sure that the file does not have more than MAXSIZE lines.

You can use fscanf instead of scanf

If you really need scanf you can send the file as input to your program with

$ ./test < test.txt

This code will work with a file and the fscanf function.

#include <stdio.h>

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
    int i;
    int a, b;
    double c, d, e;
    int A[MAXSIZE], B[MAXSIZE];
    double C[MAXSIZE], D[MAXSIZE];

    FILE* fp = fopen("teste.txt", "r");

    if(fp == NULL) {
        printf("Failed to open file.\n");
        return 1;
    }

    fscanf(fp, "%d", &i);// ignore first value
    i = 0;
    while (fscanf(fp, "%d %d %lf %lf", &a, &b, &c, &d) == 4) {
        A[i] = a;
        B[i] = b;
        C[i] = b;
        D[i] = d;

        i++;
        printf("%d %d %f %f\n", a, b, c, d);
    }
    return 0;
}
  • 1
    You can make it work with either a file provided as the first argument or (`stdin` by default) with `FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;`. The *ternary* operator is useful in many, many similar situations. – David C. Rankin Apr 21 '16 at 01:55