Sunday, June 5, 2011

File Input/Output

There are various ways to take input from file and show the result in another file. 

We can use fopen() for taking input from a file or to show output in a file. It is the easiest way to do so. First, we have to open the files and at the end we just need to close the file. An example is given below:

Suppose we want to take input from the "input.txt" file and want to store the results in the "output.txt" file. The code for this is given below:

int main(){
    int T, ans;
    int cm, y, ssu, ssa, f;
    int b, gs, gc, w;
    freopen("input.txt","r", stdin);
    freopen ("output.txt","w",stdout);
    scanf("%d", &T);
               scanf("%d%d%d%d%d", &cm, &y, &ssu, &ssa, &f);
               scanf("%d%d%d%d", &b, &gs, &gc, &w);
               ans = b + (gs/30) + (gc/25) + (w/10);
               printf("%d\n", ans);
    return 0;