C language file read/write operations, C language read/write operations

Source: Internet
Author: User

C language file read/write operations, C language read/write operations

// Kailu garji-blog Park Co., http://www.cnblogs.com/kailugaji/.

1 # include <stdio. h> 
2 # include <stdlib. h> 
3 void main () {
4 int a; 
5 void write (); 
6 void read (); 
7 while (1) {
8 printf ("******************* \ n"); 
9 printf ("1. write \ n "); 
10 printf (" 2. read \ n "); 
11 printf (" 0. exit \ n "); 
12 printf (" ******************* \ n "); 
13 printf (" Please choose: "); 
14 scanf (" % d ", & a); 
15 if (a = 0) {
16 exit (0 ); 
17} 
18 else if (a = 1) {
19 write ();
 20} 
21 else if (a = 2) {
22 read (); 
23} 
24 else
25 printf ("input e Rror! \ NPlease choose: "); 
26} 27
 28} 

29 
30 void write () {
31 FILE * fp; 
32 char ch, filename [20]; 
33 printf ("Please input the filename you want to write:"); 
34 scanf ("% s", filename); 
35 if (! (Fp = fopen (filename, "wt +") {
36 printf ("Cannot open the file! \ N "); 
37 exit (0); 
38} 
39 
40 printf (" Please input the sentences you want to write: "); 
41 ch = getchar (); 
42 ch = getchar (); 
43 while (ch! = EOF) {// EOF: Ctrl + z
44 fputc (ch, fp); 
45 ch = getchar (); 
46} 
47 
48 fclose (fp ); 
49 
50} 
51 
52 void read () {
53 FILE * fp; 
54 char ch, filename [20]; 
55 printf ("Please input the filename you want to read :"); 
56 scanf ("% s", filename);
 57 if (! (Fp = fopen (filename, "r") {
58 printf ("Cannot open the file! \ N "); 
59 exit (0); 
60} 
61 
62 printf (" % s content: ", filename); 
63 while (ch! = EOF) {// EOF (end of file): Ctrl + z
64 ch = fgetc (fp); 
65 putchar (ch); 
66} 
67 
68 fclose (fp); 
69}

Current Directory before running the program:


Program running result:


A file wrr.txt was added to the current directory.


Open the file:

 

The entered content already exists:

Hello world!
I am RONGRONG WANG!
My English name is Kailugaji!
Thank you!

 


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.