Help with Files and I/O Streams

golf_girl32's Avatar
Go4Expert Member
I need help with this program. I know the file is opening but I'm not sure how to edit the numbers and stuff that is in the file. This is what I have so far.

Code:
#include <fstream>
#include <iostream>
#include <cstdlib>

int main()
{
    using namespace std;
    ifstream input;
    ofstream output;

    input.open("input.txt");
    if (input.fail())
    {
        cout<<"Input file opening failed.\n";
        exit(1);
    }

    output.open("grade.txt");
    if (output.fail())
    {
        cout<<"Output file opening failed.\n";
        exit(1);
    }

    input.close();
    output.close();

    return 0;
}
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
send the txt file --->input.txt
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
If you want to read data from input.txt into variable 'v' and write it out to grade.txt, use:
Code:
input >> v;
output << v;
For multiple variables just use the same as you would for cin/cout, i.e.:
Code:
input >> v1 >> v2 >> v3;
output << v1 << v2 << v3; // may need to add spaces ...<<v1<<" "<<v2...