C Language File operation Exercise--A simple dictionary lookup

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.