Crash with structs loaded from file?

Okay so I was originally working on VB.Net 2010 and I was able to load the file successfully. Now I'm trying to load the file in C++ and its not working out. The structures are set-up the same. Here's the code
The First 8 bytes of the files are the length of the tDed array, then after that its all structure file.
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
#include <cstdlib>
#include <stdio.h>     // FILE structure
#include <iostream>
using namespace std;
struct tDed {
public:
string dedDate, DedAmm, DateEntered;
};

struct tPayroll {
public:
double holidaypayindex;
string CommNum, RankName, LastName, FirstName, ReferenceNumber, Dist, TotalDue,
PaycheckDed, CCDate;
tDed DedDatesAndAmmts[];
unsigned char CDNum;
string LetterSent;
bool WasChecked;
};

string getname(string s);
int main(int argc, char *argv[])
{
    cout << "Enter File Path: ";
    string s;


    getline(cin,s);
            if (s[s.length() - 1 ] == '"') s.erase(s.length() - 1,s.length());
        if (s[0] == '"') s.erase(0,1);
    cout << "Name: " << getname(s) << endl;
    system("PAUSE");
    return 0;
}
string getname(string s){
       int recordsRead;
       char z[8];
       FILE* f;
       //ifstream f(s.c_str());
              f = fopen(s.c_str(), "rb");
       if (!f) exit(0);

       recordsRead = fread(&z,7, 1,f);
       //f.read (z, 7);
       long x = 0;
for (int i = 0; i < 8; i++) if ( z[i]-'0' >= 0 && z[i]-'0' <= 9 ) x = x*10 + z[i]-'0';
          tPayroll tp; 
           realloc(tp.DedDatesAndAmmts, x );
           while ( recordsRead != 0 )
      {
         recordsRead = fread(&tp, sizeof(tp),1,f);
      }
      fclose(f);      
//           f >> (char *) &tp;
          //f.read((char *) &tp, sizeof tp);
          //cout << sizeof tp;
//           f >> ctp;
//           tp = (tPayroll*) &ctp;
//          f.close();
          return ( tp.LastName + ", " + tp.FirstName);
       }


Last edited on
Reading and writing the whole object in one go like this only works for simple types. std::string contain pointer(s) and the actual string data is stored elsewhere in memory. You will end up with a bunch of invalid string objects. Best is probably to read/write each member one by one and think carefully how you do it.
Last edited on
can you elobrate what are you trying to do .
also are you checking for the empty string over here .

1
2
if (s[s.length() - 1 ] == '"') s.erase(s.length() - 1,s.length());
        if (s[0] == '"') s.erase(0,1);


i think this is complicated way for reading the file and checking the file .
1
2
3
4
5
6
 recordsRead = fread(&z,7, 1,f);

       long x = 0;
for (int i = 0; i < 8; i++) if ( z[i]-'0' >= 0 && z[i]-'0' <= 9 ) x = x*10 + z[i]-'0';
          tPayroll tp; 
           realloc(tp.DedDatesAndAmmts, x );
In here
1
2
if (s[s.length() - 1 ] == '"') s.erase(s.length() - 1,s.length());
        if (s[0] == '"') s.erase(0,1);

it just removes quotes at the end and beginning if it came with the filename because it read as an invalid file if it had quotes.

In here
1
2
3
4
       long x = 0;
for (int i = 0; i < 8; i++) if ( z[i]-'0' >= 0 && z[i]-'0' <= 9 ) x = x*10 + z[i]-'0';
          tPayroll tp; 
           realloc(tp.DedDatesAndAmmts, x );

The first 8 characters of the file are how many tp.DedDatesAndAmmts[] are allocated to be loaded.
It looks for the first number and ends when a space ' ' is first found. I use ' ' to separate the long from the struct data.
After it gets the number from the 8 characters it converts it to a long (this part works), and then reallocates tp.DedDatesAndAmmts[0 to x-1].


There must be a way for this to work. It works perfectly fine in all Visual Basic versions D: . I tried char * instead of string, no luck. Is there something better i can use?
Last edited on
Topic archived. No new replies allowed.