How to read and write file characters, using gets (), puts (), fgets (), FPUTC () function
The Get () function is used to read a string from a standard input device (keyboard) until the line break ends, but the newline character is discarded, then the ' puts ' characters are added at the end, and used with the
Strictly speaking, gets (s) function <scanf ("%s", s), which encounters a space that would consider the end of the string
The puts () function is used to write a string to a standard output device (screen) and wrap it in a way that is called, puts (s), where S is a string character (string array name or string pointer).
Fputs is a function that has the function of writing a string to the specified file (not automatically written to the end-of-string token ' \ s '). When a string is successfully written, the position pointer of the file is automatically moved back, the function returns a value of 0, otherwise EOF (//symbol constant, with a value of-1) is returned.
Reads data from the file structure pointer stream, reading one row at a time. The data read is stored in an array of characters pointed to by BUF, reading up to bufsize-1 characters at a time (bufsize assigned ' + '),if the line in the file is less than b//ufsize characters, the line finishes reading. If the number of characters in the row (including the last newline character) exceeds bufsize-1, Fgets returns only an incomplete row, but the buffer always ends with a null character, and the next call to fgets//continues to read the row。 The function succeeds in returning BUF, failing or reading to the end of the file, returning null. Therefore, we cannot judge whether a function is terminated by an error directly through the return value of fgets, and should be judged by feof function or ferror//function.
#include <stdio.h>#include<string.h>#include<stdlib.h>intMain () {FILE*FP;Charstr[ -];if(fp = fopen ("Al","W")) ==NULL) {printf ("can not open file\n"); exit (0);} printf ("input a string:"); gets (str);p rintf ("Debug:");p UTS (str);intI=0; while(Str[i]! ='!'){if(Str[i] >='a'&&str[i] <='Z') {Str[i]= Str[i]- +; FPUTC (Str[i], FP);} I++;} Fclose (FP);if(fp = fopen ("Al","R")) ==NULL) {printf ("can not open file\n"); exit (0);} Fgets (str, strlen (str)+1, FP);p rintf ("%s\n", str); fclose (FP);return 0;}
c Read and write files