Last Updated on 2021-04-29 by Clay
Regardless of the programming language, the processing of words is important. After all, in a computer system, many settings are operated directly through text.
Therefore, the note in this section mainly record how to input and output text, as well as record read and write file at the same time.
getchar()
putchar()
fopen()
fwrite()
getchar() and putchar()
Use getchar()
to get the characters entered by the user on the keyboard. After the user presses the Enter key, the characters will be stored in the buffer, waiting for putchar()
to read.
It is worth noting that putchar()
only gets one character from the buffer at a time, and scanf()
can receive multiple types of data.
#include <stdio.h> int main() { char c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } return 0; }
Ouptut:
Today
Today
is
is
a
a
nice
nice
day
day
^C
As you can see, no matter what we input, the terminal will print out exactly the same string. Finally, I used Ctrl+C to terminate the program.
Use fopen() to read file
Suppose we have a file file.txt under the current directory, the content is:
Today is a nice day.
I want to go to play.
Then we can use fopen()
to read this file.
#include <stdio.h> int main() { FILE *fp = fopen("file.txt", "r"); char c; while ((c=getc(fp)) != EOF) { printf("%c", c); } fclose(fp); return 0; }
Ouptut:
Today is a nice day.
I want to go to play.
getchar()
usually accepts keyboard input, getc()
usually accepts file input.
Use fwrite() to write file
#include <stdio.h> int main() { FILE *fp = fopen("file.txt", "w"); char c[] = {'H', 'e', 'l', 'l', 'o'}; fwrite(c, 1, sizeof(c), fp); fclose(fp); return 0; }
Next, check whether the file.txt file has been overwritten by the new content in the terminal.
cat file.txt
Output:
Hello
(Optional) Mode of read file and write file
Mode | Description |
---|---|
r | Open file to read text data |
w | Open the file to write data. Overwrite if the file exists, create it if it does not exist |
a | Open the file to write data. If the file exists, continue to write data, if it does not exist, create |
rb | Open file to read binary data |
wb | Open the binary file to write data. Overwrite if the file exists, create it if it does not exist |
ab | Open the binary file to write data. If the file exists, continue to write data, if it does not exist, create |
r+ | Open the file in readable and writable mode |
w+ | Open the file and write data in a readable and writable way. Overwrite if the file exists, create it if it does not exist |
a+ | Open the file and write data in a readable and writable way. If the file exists, continue writing, if it does not exist, create |
rb+ | Open the binary file in read-write mode. |
wb+ | Open the binary file to write data in readable and writable mode. Overwrite if the file exists, create it if it does not exist |
ab+ | Open the binary file to write data in readable and writable mode. If the file exists, continue writing, if it does not exist, create |
References
- https://fresh2refresh.com/c-programming/c-file-handling/putchar-getchar-function-c/
- https://www.studytonight.com/c/c-input-output-function.php
- https://www.tutorialspoint.com/c_standard_library/c_function_fwrite.htm
- https://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm