fscanf c code example

Example 1: what is the function prototype for fscanf()

int fscanf(FILE *stream, const char *format, ...)

Example 2: fscanf

#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;
	
}

Example 3: scanf c

scanf("%d", &b);

Tags:

C Example