fscan C code example

Example 1: 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 2: scanf c

scanf("%d", &b);

Example 3: scanf

#include <stdio.h>
int main()
{
    int a;
    float b;

    printf("Enter integer and then a float: ");
  
    // Taking multiple inputs
    scanf("%d%f", &a, &b);

    printf("You entered %d and %f", a, b);  
    return 0;
}

Tags:

Cpp Example