fstream

I have struggled with this for a while, and I decided it was time to seek help from this splendid forum.

Here's my question:

How can I utilize fstream to save data to a file and be able to use and edit that data upon the program starting again?


Example:

In an apartment management program, you will be able to add, remove, move, and edit tenants and their apartment rooms.

This would require the utilization to save data and then be able to access and edit them once you close and restart the program.

How can I do this?

Thanks!
Last edited on
Have you tried looking at the file I/O tutorial? It has a pretty useful basic description of how to use fstreams.

http://www.cplusplus.com/doc/tutorial/files/

You can just treat them like you do cout and cin, basically, so I'm not exactly sure what part of the save/load process you are having trouble understanding.
Could you provide example code of setting and getting int values from a file?

That's what I'm struggling to understand.
Bump
> Could you provide example code of setting and getting int values from a file?

It is much easier to
a. Read the data in the file into a sequence held in memory.
b. Modify the values in memory as required.
c. Write the modified sequence back into the file.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <string>
#include <iostream>
#include <fstream>
#include <vector>

struct room
{
    room( int n, const std::string& desc ) : number(n), description(desc) {}
    int number ;
    std::string description ;
} ;

void dump_file( const std::string& path )
{ std::cout << std::ifstream(path).rdbuf() << "----------------------------------\n" ; }

int main()
{
    const std::string path_to_file = "rooms.txt" ;

    // create the file
    {
        std::vector<room> room_data { { 1, "Beginners" }, { 2, "General C++ Programming"}, { 3, "Lounge" } } ;
        std::ofstream file(path_to_file) ;
        for( const room& r : room_data ) file << r.number << ' ' << r.description << '\n' ;
    }
    dump_file(path_to_file) ;

    {
        std::vector<room> room_data ;

        // read the contents of the file into memory
        {
            std::ifstream file(path_to_file) ;
            int room_number ;
            std::string room_desc ;
            while( file >> room_number && file >> std::ws && std::getline( file, room_desc ) )
                room_data.emplace_back( room_number, room_desc ) ;
        }

        // modify stuff in memory
        for( room& r : room_data )
        {
            switch( r.number )
            {
                case 1:
                    r.number = 101 ;
                    r.description = "For people who have just started programming in C++" ;
                    break ;
                case 2:
                    r.number = 411 ;
                    r.description = "For people who have programmed in C++ for a while" ;
                    break ;
                case 3:
                    r.number = -99 ;
                    r.description = "Lounge, primarily for people who do not know C++ " ;
                    break ;
            }
        }

        // and write the modified data back
        {
            std::ofstream file(path_to_file) ;
            for( const room& r : room_data ) file << r.number << ' ' << r.description << '\n' ;
        }
        dump_file(path_to_file) ;
    }
}

Topic archived. No new replies allowed.