fscanf
#include <stdio.h>
#include <stdlib.h>
int main () {
char str1[10], str2[10], str3[10];
int year;
FILE * fp;
fp = fopen ("file.txt", "w+");
fputs("We are in 2012", fp);
rewind(fp);
fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);
printf("Read String1 |%s|\n", str1 );
printf("Read String2 |%s|\n", str2 );
printf("Read String3 |%s|\n", str3 );
printf("Read Integer |%d|\n", year );
fclose(fp);
return(0);
}
#include <stdio.h>
#include <stdlib.h>
#define N 5
int main() {
FILE *fp;
char cognome[20];
char nome[20];
int i, voto;
if((fp=fopen("alunni.txt", "rt"))==NULL) {
printf("Errore nell'apertura del file'");
exit(1);
}
for(i=0;i<N;i++) {
fscanf(fp,"%s %s %d\n", &cognome, &nome, &voto);
printf("cognome: %s, nome: %s, voto: %d\n", cognome, nome, voto);
}
fclose(fp);
return 0;
}