-
Notifications
You must be signed in to change notification settings - Fork 119
/
append_input_to_file.c
55 lines (45 loc) · 1.31 KB
/
append_input_to_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*******************************************************************************
*
* Program: Append User Input To File
*
* Description: Example of appending user input to a file in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=ABYpwVjuDN0
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX_LINE 1024
int main()
{
FILE *file;
char buffer[MAX_LINE];
// open up file.txt in APPEND mode, any data we write to the file will be
// appended to the existing content of the file
file = fopen("file.txt", "a");
// exit if there is an error opening the file
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
// continually accept lines of user input until the user enters quit
printf("Enter 'quit' to exit.\n");
do
{
// read a line of input from the terminal (stdin) and store it into buffer
fgets(buffer, MAX_LINE, stdin);
// when the user enters quit, stop
if (strcmp(buffer, "quit\n") == 0)
break;
// write the buffer to the file
fputs(buffer, file);
// accept input indefinitely
} while (true);
// close the file handle when we are done with it
fclose(file);
return 0;
}