Thread: How do you save to a text file?

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    12

    How do you save to a text file?

    Im making a small program, where you are asked questions, and all the questions and answers you give are recorded to a text file in the same folder.

    I just started C++ yesterday, so I am pretty much a newbie. But if you could help me then thank you.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Once you've got an open file, you write to it just like you would write to cout:
    Code:
    #include <fstream>   // For ofstream
    #include <iostream>  // For cout
    #include <cstdlib>   // For EXIT_SUCCESS and EXIT_FAILURE
    using namespace std;
    
    int main()
    {
        // Create an output file object called "myfile" that opens a file called "file.txt"
        ofstream myfile("file.txt");
        
        if( !myfile.is_open() )
        {
            cout << "Error: Could not open output file." << endl;
            return EXIT_FAILURE;
        }
    
        cout << "This line of text will get sent to cout." << endl;
        myfile << "This line of text will get sent to the file." << endl;
    
        return EXIT_SUCCESS;
    }
    File objects will close themselves automatically when they go out of scope, i.e. at the end of main in this case.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    Cheers, I will look through it and work it out

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    Oh yeah, just another thing, how do you clear screen in C++...

    I thought it was CLS?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What system and compiler are you using? There's a FAQ entry for this.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    Cheers for that link, all solved

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    I need a little help, I am making a small profile program, simple (but im new...) and when you enter the information of the questions it saves to a file called profile.txt, this is all fine.

    But if you enter a answer with SPACES in like "Computers and Games", it will only record Computers and nothing else after.

    How do I prevent this.

    This is my code so far,

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <stdlib.h>
    
    using namespace std;
    
    char myname[100];
    char myinterests[101];
    
    int myage;
    
    int main()
    {
        ofstream myfile("profile.txt");
        
        if( !myfile.is_open() )
        {
            cout << "Error: Could not open output file." << endl;
            return EXIT_FAILURE;
        }
    
    
    	cout<<"What is your name?"<<endl;
    	cin>>myname;
    
    	cout<<"What is your age?"<<endl;
    	cin>>myage;
    
    	cout<<"What are your interests?"<<endl;
    	cin>>myinterests;
    
    	system ("cls");
    
    	cout<<"Your profile has been recorded to profile.txt"<<endl;
    
    	cout<<""<<endl;
    
    	cout<<"Your Profile:"<<endl;
    
    	cout <<"Hello, my name is "<<myname<< " and I am "<<myage<< ". My interests are "<<myinterests<<endl;
    
        myfile << "Hello, my name is "<<myname<< " and I am "<<myage<< ". My interests are "<<myinterests<<endl;
    	
        return EXIT_SUCCESS;
    }

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    lookup std::noskipws in your helpfiles.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    Hmm, nothing. Confused

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can also use getline to get a line of text, including spaces. You must be careful when mixing getline and operator>>, though. Quick solution - add a cin.ignore() after each call to cin >> if you want to also use getline().

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    Quote Originally Posted by Stoned_Coder
    I only started CPP yesterday, so that link kind of confuses me?

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    Quote Originally Posted by Daved
    You can also use getline to get a line of text, including spaces. You must be careful when mixing getline and operator>>, though. Quick solution - add a cin.ignore() after each call to cin >> if you want to also use getline().

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Simple answer: Use getline. It's a function from cin that reads in an entire line instead of stopping at the first space.

    Extra Info: If you decide to use getline, but run into problems (like it reads in an empty string) when using it after cin >>, then be aware that you can call cin.ignore() after cin >> statement.

    If you are confused, a specific question would be easier to answer. Make sure you've done a little research first.

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Holy cow dung. Its not hard. Just read the section about manipulators. You used endl? its pretty similar and that way because all your input ops are formatted input you dont get involved in the crap of whether theres anything left in the stream or not. Thats why I didn't recommend getline.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM