MPGHCC: How to File Input

Posts 1–3 of 3 · Page 1 of 1
MPGHCC: How to File Input
Ok since many people cant file input ;D im going to make a thread on inputting data through a file, specifically for the MPGHCC. There are two ways to do it it but i prefer the first one:

Method One:
Code:
#include <iostream>

using namespace std;

int main(void){

freopen("input1.txt","r",stdin);
int i;
for(i=0;i<5;i++) //for all 5 test cases
{
    int n;
     cin>> n; //input n from file
     cout<<n*3<<endl; //output to screen

}
}
Method two:
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main(void){

ifstream fin("input1.txt");
int i;
for(i=0;i<5;i++){
    int n;
    fin>>n; //read from file
    cout<<n*3<<endl; //output to screen

}

}
I prefer fstream and simply specifying rather I want input or output or other flags set.
Code:
fstream file("text.txt", ios::in | ios::out);
well those are the easy ways ^ ^ mr. overcomplciated
Posts 1–3 of 3 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us