writing/reading txt file

This is a discussion on writing/reading txt file within the C++ Programming forums, part of the General Programming Boards category; this would be used alot in console rpgs that let you save your stats. i want to be able to ...

  1. #1
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608

    writing/reading txt file

    this would be used alot in console rpgs that let you save your stats. i want to be able to write to a txt file a bunch of my variables and then be able to read the txt file and making variables so its a save/load type thing. i searched google and the forums couldnt find anything. any help thanks in advance
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    read the FAQ
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void save(int i, double d, const string &str)
    {
        ofstream fout("save.txt"); // no error checking
        fout << i << endl
             << d << endl
             << str << endl;
        fout.close();
    }//save
    
    void load(int &i, double &d, string &str)
    {
        ifstream fin("save.txt"); // no error checking
        fin >> i >> d;
        fin.ignore(1); // skip newline
        getline(fin ,str);
        fin.close();
    }//load
    
    int main()
    {
        int i = 42;
        double d = 3.1415926535897932384626433832795;
        string str = "life is like a piece a pie";
    
        save(i, d, str);
    
        i = 0;
        d = 0;
        str = "";
    
        load(i, d, str);
    
        cout << i << endl << d << endl << str << endl;
    
        return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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