How to write a C Program File Input & Output Example in C Programming Language ?
Solution For C Program :
//C Program File Input & Output.
#include <stdio.h>
int main(){
char inputFilename[80];
char outputFilename[80];
gets(inputFilename);
gets(outputFilename);
/*
* Read data from the file:
1. open the file
2. Read file
3. Close the file
*/
FILE *input;
input = fopen(inputFilename, "r");
int n;
fscanf(input, "%d", &n);
int num[1005];
int i;
for( i = 0 ; i < n ; i++ ){
fscanf(input, "%d", &num[i]);
}
fclose(input);
/* Total count */
int sum = 0;
for( i = 0 ; i < n ; i++ ){
sum += num[i];
}
/*
* Write data to a file:
1. open the file
2. Write file
3. Close the file
*/
FILE *output;
output = fopen(outputFilename, "w");
fprintf(output, "%d", sum);
fclose(output);
return 0;
}