C Exercises: File read and write using variable
C File Handling : Exercise-17 with Solution
Write a program in C to read from a file into a variable and write a variable's contents into a file.
Sample Solution:
C Code:
#include <stdio.h>
int main(int argc, char **argv) {
FILE *in, *out;
int c, str;
in = fopen("i.txt", "r");
if (!in) {
fprintf(stderr, "Error opening i.txt for reading.\n");
return 1;
}
out = fopen("o.txt", "w");
if (!out) {
fprintf(stderr, "Error opening output.txt for writing.\n");
fclose(in);
return 1;
}
while ((c = fgetc(in)) != EOF) {
fputc(c, out);
}
fclose(out);
fclose(in);
return 0;
}
Sample Output:
Error opening i.txt for reading.
Flowchart:
C Programming Code Editor:
Previous: Remove a file from the disk.
Next: File modification time.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming-exercises/file-handling/c-file-handling-exercise-17.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics