The fscanf () function works as scanf (). The difference is that fscanf () reads a file and not the computer keyboard. prototype:
int fscanf (FILE * fp, char * str, …);
How could we expect, the only difference from the prototype fscanf () to the scanf () is the specification of the target file through the file pointer. Perhaps the simplest way to write the program is using fprintf () and fscanf (). Looks like this:
The fprintf () function works like printf () function. The difference is that the output of fprintf () is a file and not the computer screen. prototype:
int fprintf (FILE * fp, char * str, …);
How could we expect, the only difference from the prototype fprintf () to the printf () is the specification of the target file through the file pointer.
The program given below generates prime numbers in a given range and appends them to primes.dat file. Then it displays the primes.dat file.
/* append prime numbers in a given range to primes.dat file and then display the file */ #include <stdio.h> #include "fileopen.c" int is_prime(int n); int main () { char fname[] = "primes.dat"; FILE *fp; int m, n, i; /* add prime numbers in a given range to primes.dat file */ printf("Enter range : "); scanf ("%d %d", &m, &n); fp = fileopen(fname, "at", ""); for (i = m; i <= n; i++) { if (is_prime(i)) fprintf(fp, "%d ", i); } fclose (fp); /* display contents of primes.dat file */ fp = fileopen(fname, "rt", ""); printf("Prime numbers in primes.dat file:\n"); while (fscanf (fp, "%d", &i) != EOF) printf("%d ", i); fclose (fp); return 0; /* test if n is a prime number */ int is_prime(int n) int d; for (d = 2; d < n; d++) { if (n % d == 0) break; } return (d == n) ? 1 : 0; }
The program accepts a range from the keyboard, opens primes.dat as a text file in append mode, writes prime numbers in the specified range to it using a for loop and then closes the file. Observe the use of the fprintf function to write the prime numbers.
The primes.dat file is then opened again in read mode and a while loop is used to read and display its contents. Observe the use of the fscanf function to read the numbers from the file. The output obtained when the program is executed for the first time is given below.
Enter range : 1 20
Prime numbers in primes.dat file:
2 3 5 7 11 13 17 19
The program is executed again and the output obtained is given below.
Enter range : 21 40
Prime numbers in primes.dat file:
2 3 5 7 11 13 17 19 23 29 31 37
Although the program works as expected, note that primes.dat file is opened and closed twice in each program execution. This overhead can be eliminated by opening the file in update mode (“wt+”);this allows both read and write operations on a file. The modified program is given below. Observe the use of rewind function to reposition the file pointer to the beginning of the file after the prime numbers are written to it (only the main function is given to save space).
int main() { char fname[] = "primes2.dat"; FILE *fp; int m, n, i; /* add prime numbers in a given range to primes.dat file */ printf("Enter range : "); scanf("%d %d", &m, &n); fp = fileopen(fname, "at+", ""); for(i = m; i <= n; i++) { if (is_prime(i)) fprintf (fp, "%d ", i); } rewind(fp); printf("Prime numbers in primes.dat file:\n"); while (fscanf(fp, "%d", &i) != EOF) printf(“%d ", i); fclose(fp); return 0; }