Daffodil International University

IT Help Desk => Programming Language => Programming Competition => Topic started by: Nazmul on March 03, 2012, 01:06:10 PM

Title: Introduction to Programming Contest (File Input/Output)
Post by: Nazmul on March 03, 2012, 01:06:10 PM
Giving input form keyboard sometime become hazy during contest time. So, we use file i/o. Here i am going to give a sample code which takes inputs from file and produces output to file (text file)

Suppose we want to take input from the "input.txt" file and want to store the results in the "output.txt" file.
You will have to include one function call before taking first input:
Code: [Select]
freopen("input.txt","r", stdin); freopen function opens the "input.txt" data file in read mode "r" and sends it to standard input (stdin)

Place another freopen call for putting the outputs into file as below:
Code: [Select]
    freopen ("output.txt","w",stdout);it opens the "output.txt" file into write mode "w" and acts as standard console output (stdout)

Important note: Never forgot to close these streams before end of the program

Code: [Select]
    fclose(stdin);
    fclose(stdout);
    return 0;
}

Now your program is ready to take input from file and produce output into file.
Title: Re: Introduction to Programming Contest (File Input/Output)
Post by: jas_fluidm on March 08, 2012, 11:31:24 AM
thanks for the post