Help with Files and I/O Streams

Discussion in 'C++' started by golf_girl32, Oct 12, 2010.

  1. golf_girl32

    golf_girl32 New Member

    Joined:
    Sep 28, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
    
    
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    send the txt file --->input.txt
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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...
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice