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;
}
Program related to C Programming Language :
- C Program Array Index Example
- C Program String - Alphabet Count Example
- C Program Character toupper() Example
- C Program Array Example: Average
- C Program Array Example: Reverse
- C Program if - while - for Example
- C Program Switch - Case Example
- C Program 0.1 + 0.2 != 0.3 Solved
- C Program if - else if - else Example
- C Program typedef Example
- C Program scanf & gets Example
- C Program Friend & Operator: Point2D Example
- C Program Friend & Operator: Vector Example
- C Program Recursion Example
- C Program File Input & Output Example
- C Program Structure Example-2
- C Program Structure Example
- C Program Pointer Example
- C Program Function Example
- C Program String Example
- C Program String Count Example
- C Program String Example
- C Program Character Example
- C Program Sort Example
- C Program sizeof & memcpy Example
- C Program Array Example
- C Program to Array
- C Program print fill zero & mod/div
- C Program Double, Float Equal
- C Program nested for
- C Program for
- C Program if
- C Program System
- C Program Operation
- C Program to printf & scanf
- Example C Program Cuda Malloc
- C Program Side Length
Learn More :
Input
- C Program to Get User Input and Compare
- C Program Basic logging of user input/output
- C Program To Input & Print More Than One Words In Single Line
- Take microphone input and send to headphones
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program That in a Given Natural Number x Insert Figure c at Position p
- Input reads in the array positions and range for random generation.
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to Input Number by the user between 1-100
- How to Gets User Input For Height in C Programming
- DEMULTIPLEXER CO ANALOG INPUTS
- C Program to Simulation program for FCFS ( First Come First Serve )
- C Program to Input Student Details into a File For Two Subjects
- Program to Display Pie Chart Accepting User Input C Program
- Input a Number and Check if it's Palindrome or not C Program
- Input Number and Print it's Reverse C Program
- Input Number and Check if it's Armstrong C Program
- Input Number and Check if it's Even or Odd C Program
- Input Two Numbers and Print Greater Number C Program
- Input Number and Calculate Sum of it's Digits C Program
Output
- DISPLAY SOURCE CODE AS OUTPUT IN C PROGRAM
- C Program to Save output of system command to variable with popen
- C Program Basic logging of user input/output
- C Program To Print String In Formatted Output Form
- C Program Nested Loop (inverted Triangle) C Code
- A small I/O bound program to copy N bytes from an input file to an output file.
File
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program To Destruc Self Execution File ?
- C Program To Store Students Record In A Text File
- C Program To Write Data In A File And Search Data From File
- Make a copy of file given in argv[1] to file given in argv[2]
- C Program to copy a file to another file
- C Function to read instructions from the file and obey them
- Client/Server C program to make client send the name of a file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Read and Write FIFO
- File Number Control C Program Example
- C Program to opens and reads dictionary file specified in spellrc
- C Program to Recursively converts all file-names to lower-case
- C Program to Find the Size of File using File Handling Function
- C Program Recursive function that searches for a given file in a given folder
- C Program Read a char & print next char ( file to file )
- C Program to Input Student Details into a File For Two Subjects
- Identifies the architecture Windows PE (exe or dll) file was compiled C Program
- File Handling (console to file) in C Program