Reading A Binary file into a vector

My program writes a vector to a file in binary. I want to erase the vector, then read the file to repopulate the vector then display.

Basically I want to erase the RAM memory and be able to use the file as memory. This is my current code but it is not reading the file correctly. I have checked the file to see that the writing part is working.

Any ideas?

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

void read_from_file(vector<Info> &vector)
{
     
     fstream file;
     
     file.open("info.dat", ios::binary | ios::in);
     
                if (file.fail())
                {
                       cout<<"\n FILE DOES NOT EXIST \n";
                       system("pause");
                }
                
                if(file.good())
                {
     
                       for (int count = 0;count < vector.size(); count++)
                       {
                             file.read(reinterpret_cast<char*>(&vector[count]),sizeof(vector[count])); 
                       }
           
                       for (int count = 0; count < vector.size(); count++)
                       {
                            cout << endl << (count) << ". NAME: " << vector[count].name << endl;
                            cout << (count) << ". AGE: " << vector[count].age << endl;
                       }
     
                }
     
     cout<<"\nINFORMATION READ FROM THE FILE\n";
     
     file.close();
}
Is the size of the vector the same as the size of the file?
Yes. For example I put one record in element one and one record in element two. I see them in binary in the file.

When I hit this function. It does not display the information in the files, so I am assuming it is not reading the file into the vector.

*I am deleting the vector before I read it back

Does the logic and syntax seem correct??
Last edited on
¿what's `Info'?
I would post the entire program but its huge. This is the structure with Info.

1
2
3
4
5
6
7
8
    const int name_size = 20;

    struct Info{

                    char name[name_size];
                    int age; 

               };


In the main function I declare the vector

1
2
3
4
5
6
7
int main()
{

    vector<Info> vector, vector1;

......
Another thread is here:
http://www.cplusplus.com/forum/beginner/122329/
It is very very rude to post multiple times, please do not do it again.
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
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>

struct info
{
    static constexpr std::size_t name_size = 32 ;
    char name[name_size];
    int age;

    info() {}
    info( const char* n, int a ) : age(a) { std::strcpy( name, n ) ; }
};

int main()
{
    // create a test vector
    std::vector<info> test = { { "one", 1 }, { "two", 2 }, { "buckle", 100 },
                               { "my", 101 }, { "shoe", 102 }, { "three", 3 } };

    // print out its contents
    for( const info& inf : test )
        std::cout << "{ " << inf.name << ", " << inf.age << " } " ;
    std::cout << '\n' ;

    {
        std::ofstream file( "info.dat", std::ios::binary ) ; // output file stream

        // dump the contents of the vector in the file
        for( const info& inf : test )
            file.write( reinterpret_cast<const char*>( &inf ), sizeof(inf) ) ;
    } // the file is flushed and closed

    test.clear() ; // clear the vector

    {
        std::ifstream file( "info.dat", std::ios::binary ) ; // input file stream

        // read the contents of the file into the vector
        info inf ;
        while( file.read( reinterpret_cast<char*>( &inf ), sizeof(inf) ) )
            test.push_back(inf) ;
    }

    // print out the contents of test
    for( const info& inf : test )
        std::cout << "{ " << inf.name << ", " << inf.age << " } " ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/c487c96da4a4fe83
Sorry about posting twice. Thank you.

EDIT: I forgot to put the push_back command after reading from the file. Works fine now. Thank you everyone.

Last edited on
Topic archived. No new replies allowed.