Introduction to Programming Contest (File Input/Output)

Author Topic: Introduction to Programming Contest (File Input/Output)  (Read 4786 times)

Offline Nazmul

  • Faculty
  • Newbie
  • *
  • Posts: 35
  • Senior Lecturer (On Study Leave), Dept. of CSE
    • View Profile
    • Nazmul Haque's WebHome
Introduction to Programming Contest (File Input/Output)
« 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.
Mohammad Nazmul Haque
Senior Lecturer
(On Study Leave)
Dept. of CSE,CIS & CS
Daffodil International University
102 Sukrabad,Mirpur Road,
Dhaka-1207,Bangladesh

email: nazmul@daffodilvarsity.edu.bd
web: http://daffodilvarsity.edu.bd/faculty/nazmul/

Offline jas_fluidm

  • Faculty
  • Sr. Member
  • *
  • Posts: 291
    • View Profile
Re: Introduction to Programming Contest (File Input/Output)
« Reply #1 on: March 08, 2012, 11:31:24 AM »
thanks for the post