i'm currently doing an uni project which has to read a multiple lines sequence of inputs given in a .txt format. This is my first experience with C, so i don't know much about reading files with fscanf and then processing them. The code i wrote goes like this:
#include <stdio.h>
#include <stdlib.h>
int main() {
char tipo [1];
float n1, n2, n3, n4;
int i;
FILE *stream;
stream=fopen("init.txt", "r");
if ((stream=fopen("init.txt", "r"))==NULL) {
printf("Error");
} else {
i=0;
while (i<4) {
i++;
//i know i could use a for instead of a while
fscanf(stream, "%s %f %f %f %f%", &tipo, &n1, &n2, &n3, &n4);
printf("%s %f %f %f %f", tipo, n1, n2, n3, n4);
}
}
return 0;
}
My "init" file is formatted like this:
L 150.50 165.18 182.16 200.50
G 768.12 876.27 976.56 958.12
A 1250.15 1252.55 1260.60 1265.15
L 200.50 245.30 260.10 275.00
A 1450.15 1523.54 1245.17 1278.23
G 958.12 1000.65 1040.78 1068.12
I don't know how to tell the program to skip a line after the first one is read.
Thanks for the help in advance!
There's no reason to use a char array (string) when you're only going to read one character.
Do this:
char tipo;
and
fscanf(stream, " %c %f %f %f %f%", &tipo, &n1, &n2, &n3, &n4);
and your code should work. Note the c
instead of s.