For instructions on using keyboard input files! In a hurry !!

Source: Internet
Author: User
For instructions on using keyboard input files! In a hurry !! -- Linux general technology-Linux technology and application information. For details, refer to the following section. There is a linux c question, let me write two programs, a program requires three people's name and account balance information through a flow file I/O operations written into the file "test-4-10file ", in another format, the account information is output, and the account and balance meanings of each person are displayed. (The account and balance are input on the keyboard, and the balance can be decimal)

1. I wrote the test-4-10fwrite.c and test-4-10fread.c as follows:
Test-4-10fwrite.c:
# Include
# Include
# Define nmemb 3
Struct test
{
Char name [20];
Double pay;
} S [nmemb];
Int main ()
{
FILE * fp;
Int I;
Double f;

If (fp = fopen ("test-4-10file", "a +") = NULL)
{
Printf ("An error occurred while opening (creating) the file ");
Exit (0 );
}
For (I = 0; I {
Printf ("Enter the account :");
Fgets (s . Name, 20, stdin );
Printf ("Enter the balance :");
Scanf ("% lf", & s. Pay );
}
Fwrite (s, sizeof (struct test), nmemb, fp );
Fclose (fp );
Return 0;
}
/* Optional /*---------------------------------------------------------------------------------------------*/
Test-4-10fread.c:

# Include
# Define nmemb 3
Struct test/* define struct */
{
Char name [20];
Int pay;
} S [nmemb];
Int main ()
{
FILE * fp;/* define the FILE variable pointer */
Int I;
Fp = fopen ("test-4-10file", "r");/* open the file */
Fread (s, sizeof (struct test), nmemb, fp);/* call the fread function to read block information from the file */
Fclose (fp);/* close the file stream */
For (I = 0; I Printf ("account [% d]: %-20 s balance [% d]: % d \ n", I, s . Name, I, s. Pay );
Return 0;
}
/* Optional /*--------------------------------------------------------------------------------------------*/

2. then compile
Gcc test-4-10fwrite.c-o test-4-10fwrite
Gcc test-4-10fread.c-o test-4-10fread

3. Final Run
./Test-4-10fwrite/run results below/
Enter account: James
Enter balance: 12345
Enter account: Enter balance: 200
Enter account: Enter balance: 100
/Test-4-10fread
Account [0]: James
Balance [0]: 0
Account [1 @
Balance [1]: 0
Account [2]: balance [2]: 0
/*-------------------------------------------*/
Question: for (I = 0; I {
Printf ("Enter the account :");
Fgets (s. Name, 20, stdin );
Printf ("Enter the balance :");
Scanf ("% lf", & s. Pay );
}
Why do I enter an account (that is, fgets (s) in this loop?. Name, 20, stdin) only once?
How should I change it?
The result I want is:
/Test-4-10fwrite
Enter account: James
Enter balance: 12345
Enter the account: Li Si
Enter balance: 200
Enter the account: Wang Wu
Enter balance: 50000
/Test-4-10fread
Account [0]: Zhang San balance [0]: 12345
Account [1]: Li Si balance [1]: 200
Account [2]: Wang Wu balance [2]: 50000

Large? Make a fuss about it ..
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.