Recently, many of the exercises that have been taken into account are related to file reading.
Function used: Freopen () The following explanation comes from the Baidu Encyclopedia:
Freopen is a function that is included in the C standard library header file <stdio.h> for redirecting input and output streams. The function can change the input and output environment without changing the original code, but should be used to ensure that the flow is reliable.
---------------------------------------
This function can be used to redefine the input of scanf to a file which is very useful when testing large amounts of data.
#include <stdio.h> #include <string.h>int main () {char word[80],tmp[80],mean[100];start:while (" What ' s the word? ")) {freopen ("CON", "R", stdin),//windows Restore keyboard input//freopen ("/dev/tty", "w", stdout);//Other System scanf ("%s", TMP); Freopen (" A.dat "," R ", stdin), while (scanf ("%s%s ", Word,mean)!=eof) {if (strcmp (word,tmp) ==0) {printf (" The word means:%s\n ", mean) ; goto start;}} printf ("The Word not found!\n");}}
A.dat
A 1
B 2
C 3
Because it's too low to read, and it's better to change to binary.
C Language File operation Exercise--A simple dictionary lookup