Writing to a file. But file becomes huge!

Hello guys, just getting into reading/writing to a file using the ofstream in c++. Even though I am successful in doing so and reading back from the file is ok, the file itself when viewed in windows explorer is over 300mb large!

And this was after I entered less text to store into the file. Originally it ended up being 1.12 gb large! :O

I am puzzled at the size, so I am hoping someone can point out what I am doing wrong. The following code is just a sample program to test the file writing/reading after researching the net.

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
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

struct employee
{
    int id;
    string name;
    int age;
    string address;
} emp;

main()
{
    ofstream file("employer.dat", ios::out|ios::binary);
    if (!file)
    {
        cout << "Error opening file, creating a new file\n";
        file.clear();
        file.open("employer.dat", ios::out|ios::binary);
    }

    try
    {
        cout<<"Enter your id # ";
        cin>>emp.id;
        cout<<"Enter your name ";
        cin.ignore();
        getline(cin,emp.name);
        cout<<"Enter your age ";
        cin>>emp.age;

        if(emp.age <=0)
           throw emp.age;
        cout<<"Enter your address ";
        cin.ignore();
        getline(cin,emp.address);

    }

    catch(int e)
    {
        cout << e << " Sorry this age is not acceptable\n";
        Sleep(5);
        exit(1);
    }


    file.seekp((emp.id * sizeof(emp)), ios::beg);
    file.write(reinterpret_cast<char*>(&emp),sizeof(emp));
    file.close();

    ifstream fin("employer.dat", ios::in|ios::binary);
    cout<<"Enter the employee id you want to view ";
    cin>>emp.id;

    fin.seekg((emp.id * sizeof(emp)), ios::beg);
    fin.read(reinterpret_cast<char*>(&emp),sizeof(emp));
    cout<<"Id is ";
    cout<<emp.id<<endl;
    cout<<"Name is ";
    cout<<emp.name<<endl;
    cout<<"Age is ";
    cout<<emp.age<<endl;
    cout<<"Address is ";
    cout<<emp.address<<endl;

    fin.close();
    system("pause");
}


The output is below:
Enter your id # 20006348
Enter your name doc archy
Enter your age 30
Enter your address 7 bird street
Enter the employee id you want to view 20006348
Id is 20006348
Name is doc archy
Age is 30
Address is 7 bird street
Press any key to continue . . .
The file with the above data, ended up being 306mb large.

Is this the norm?
Thanks for any help.
Last edited on
What in the world are you trying to do?
file.write(reinterpret_cast<char*>(&emp),sizeof(emp));
I've never seen anyone do something like this before.

Can't you just use something like file << "This string will be written to the file.";
and be done? (it works with ints, too, in case you didn't know)

With that in mind, I don't see why you need to open the file as a binary file.
So I would consider removing that, too...
That is how you write data to a binary file, though I've been using something like this: object.write((char*)&i, sizeof(i)); instead.

Line 53 might be trouble, you set the position of the stream buffer to 20006348 * whatever the size of the emp would be.

btw, is that how you could quickly write the entire contents of a struct? Or is Long Double Main closer to the truth?
Last edited on
LowestOne, I think you might just be right about line 53. I set the id to 20, and it created a 336 byte file instead.

And yes, that is how you could quickly write the entire contents of a struct.
LowestOne is right in that line 53 is the problem. All the bytes you skip by doing seekp will be part of the total file size. If it actually takes up all that space on the hard disk depends on you OS.

Another problem with your code is that you can't write an std::string to file like that. Internally std::string contains a pointer to the actual string data but only the pointer value will be written to the file.
Last edited on
And yes, that is how you could quickly write the entire contents of a struct.


hmm... Thanks.
Topic archived. No new replies allowed.