top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can we take input from a file and print output in the file in C programming?

+3 votes
465 views
How can we take input from a file and print output in the file in C programming?
posted Apr 9, 2016 by Shahsikant Dwivedi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Use the following commands -
fscanf - for the input of the program from the file.
fprintf - for the output to be written to the file.
used freopen in read and write mode, its working.

1 Answer

+1 vote
 
Best answer

try this method,
1. freopen("NameOfInputFile.in","r",stdin); // set input file in read mode and from this file in a particular format the compiler will read all inputs and
2. freopen("NameOfOutputFile.out","w",stdout); // print output in this .out file.
Use both of them like:

int main()
{
 freopen("NameOfInputFile.in","r",stdin); 
 freopen("NameOfOutputFile.out","w",stdout); 
 // start code here 
.
.
.
return 0;
}
answer Apr 11, 2016 by Shivam Kumar Pandey
Similar Questions
0 votes

Write a C code for the logic -
input: string, strong
output: strng

+2 votes

Input:
arr = {'a', 'b'}, length = 3

Output:
aaa
aab
aba
abb
baa
bab
bba
bbb

+3 votes

Implement a code that counts the occurrences of each word in an input file and write the word along with corresponding count in an output file sorted by the words alphabetically.

Sample Input

Gaurav is  a Good Boy
Sita is a Good Girl
Tommy is  a Good Dog
Ram is a Good person

Sample Output

D:\>Java FileWordCount inputFile.txt outputFile.txt
    Boy : 1
    Dog : 1
    Gaurav : 1
    Girl : 1
    Good : 4
    Ram : 1
    Sita : 1
    Tommy : 1
    a : 4
    is : 4
    person : 1
...