About io stream!

This is a discussion on About io stream! within the C++ Programming forums, part of the General Programming Boards category; i'm writing a program for the io stream one.i wanna to save a file after i wrote all the output ...

  1. #1
    Unregistered
    Guest

    About io stream!

    i'm writing a program for the io stream one.i wanna to save a file after i wrote all the output inside the file. Which i called "Save"? and then,i able to save all the output in that file i created called "Save". but for now i still can't write this simple program..... pls give some direction on how to do it? thanks

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    When you open a file for output (write to file) all writting operations happen on demand. You don't save the file. You simply open it, write to it and close it.

    Just to guide you on what you should be reading about, the following code opens a new file, writes 2 lines and closes it.

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        //creates a pointer to a file. ofstream since we will be using '<<' in order to write to it.
        ofstream pfile;
        
        //open/create the file. File is named teste.txt. 'ios:out' because we want to write to the file
        //If it already exists, it's created anew and everything on it is lost.
        //If it doesn't exist, it is created.
        pfile.open("teste.txt", ios::out);
        
        //the following tests if the file exists. IMPORTANT!!
        if(!pfile)
        { 
            cout << "Error opening file. Exiting..."<< endl;
            system("pause");
            exit(1);
        }
    
        //now lets just write to the file. 3 lines in all.
        pfile << "This is line 1" << endl;
        pfile << "This is line 2" << endl;
        pfile << "This is line 3";
        
        //Close the file when you no longer need it. IMPORTANT!!
        pifile.close();
    
        return 0;
    }
    Alas, you just created a file. It should be on the same folder as your source. From here just try and grab some tutorial on file I/O. Don't use the one on this site. It's buggy

    [EDIT]
    Just replace the smiley you see on the code for a ':' followed by an 'o' (that's the letter o)
    [/EDIT]
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Registered User geekfrog's Avatar
    Join Date
    Apr 2002
    Posts
    14
    For example, you could try The file i/o tutorial on this site

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 01:30 AM
  2. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 04:15 PM
  3. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  5. Totally puzzling IO problem
    By Vorok in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2003, 06:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21