Write to data file

Hi, I am trying to write an entire struct into the binary data file.

I have programmed this successfully in the past(way back) and I've also checked all on the internet but I cannot see the error in my code. For some reason this fails to write anything to my "business.dat" file. It is bugging me soo much since I know that my error is something small and very technical that I forgot.

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
    Company write;

    write.CompCode      = (numRecords("businesess.dat") + 1);
    write.iEmployees    = 1;
    write.NumApps       = 0;
    write.NumFounders   = 1;
    write.Sector        = "ff";
    write.sName         = "ff";
    write.valuation     = 1;


    fstream file;

    file.open("businesess.dat",ios::out|ios::binary|ios::app);
    file.seekg(numRecords("businesess.dat")*sizeof(write),ios::beg);
    file.write(reinterpret_cast<char*>(&write),sizeof(write));    
    file.close();


    int numRecords(const char* file)
{
    fstream Myfile;

    Myfile.open(file,ios::binary|ios::out);
    Myfile.seekg(0,ios::end);
    int num = Myfile.tellg();
    Myfile.close();
    num = num / sizeof(Company);
    return num;
}

#pragma pack (push)
struct Company
{
    string  sName;
    int valuation;
    string Sector;
    int iEmployees;
    int NumFounders;
    int NumApps;
    int CompCode;
};
#pragma pack(1)
#pragma pack(pop) 
You have two strings embedded in your struct. String is a complex data type containing a pointer to the memory that holds the string. When you try to write a std::string in this manner, you're writing the pointer, not the data.
Ahh yes of course. I have now reimplemented it in the simplest char array I could quickly think of. But I still get nothing written to the .dat file. I only am gonna repaste code I changed.

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
    write.CompCode      = (numRecords("businesess.dat") + 1);
    write.iEmployees       = 1;
    write.NumApps          = 0;
    write.NumFounders   = 1;
    write.Sector[0]          = 'f';
    write.sName[0]         = 'f';
    write.Sector[1]          = 'f';
    write.sName[1]         = 'f';
    write.valuation          = 1;

    fstream file;

    file.open("businesess.dat",ios::out|ios::binary|ios::app);
    file.seekg(numRecords("businesess.dat")*sizeof(write),ios::beg);
    file.write(reinterpret_cast<char*>(&write),sizeof(write));
    file.close();



#pragma pack (push)
struct Company
{
    char  sName[2];
    int valuation;
    char Sector[2];
    int iEmployees;
    int NumFounders;
    int NumApps;
    int CompCode;
};
#pragma pack (1)
#pragma pack(pop) 

*update* I have also tried the struct without any char arrays/strings and still same result
Last edited on
Are you sure you're file is actually opening?

Are you sure your seekg() is not failing?

Are you sure your write() call is not failing?

You really should check the stream state after each of these calls to insure that the operated "correctly".

Can you write to an empty file using an ofstream with just the std::ios::binary flag?

How are you checking that the information didn't get written to the file?
Thank you jlb. you make me think and I found the error. All was perfect excep in my numRecords function I should define ios::in instead if ios::out. That cleared the data file everytime i wanted to count the records.
Topic archived. No new replies allowed.