strings >> array >> file

1
2
3
4
5
6
7
                           for(int u=0; u<5; u++)
                           {
                           Product.read((char*)&Array1[u], sizeof (Commodity));
                           cout<<"ID: "<<Array1[u].GetID()<<endl;//unsigned long long
                           cout<<"Day: "<<Array1[u].GetDay()<<endl;//int
                           cout<<"Name: "<<Array1[u].GetName()<<endl;//string ->crash line
                           }

Every element of this array contains an obect of class and is saved to file with extension (.dat). When i try to read the name(string), saved inside the first element of the array the program just crashes.I can read int, double unsigned long long...generally everything except of the strings. Does anybody have an idea what am i supposed to do to get proper results?
Last edited on
I'm not completely sure but i think that code isnt wrong. It cud be in ur .dat file. There isnt enuff data to read. unsigned long long takes 8bytes. Can u show more code?
It entirely depends on the class definition. Show it.

I'll guess: Your strings are of type std::string.
closed account (zb0S216C)
el3m3ntal wrote:
cout<<"Name: "<<Array1[u].GetName()<<endl;//string ->crash line

If GetName( ) returns a string object, calling c_str( ) may solve your problem. For example:

cout << "Name: " << Array1[ u ].GetName( ).c_str( ) << endl;

Wazzak
Last edited on
Would you please send your whole code??

i think you have a problem with you "result type" of your member function, is that like these??
1
2
3
4
5
6
7
8
9
char & getName( void )
{
...
}
//or
char * getName ( void )
{
...
} 
closed account (zb0S216C)
ashkan wrote:
char & getName( void )

It's unlikely that it's returning a reference to a single character. However, the method name implies that it returns a string of some type. If you changed that line of code with this: char *&GetName( void ), it would work. What that code does is return a reference to a pointer, if you didn't already know.

ashkan wrote:
char * getName ( void

If this was the case, the method should work, but it doesn't.

Wazzak
Last edited on

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
class Commodity
{
      private:
              unsigned long long ID;
              int Day;
              int Month;
              int Year;
              double Price;
              string Name;
              public:
                     Commodity(){};
                    Commodity( unsigned long long kID,string kName,int kDay, int kMonth, int kYear, double kPrice);
                     void SetName(string fName){Name=fName;}
                     void SetID(unsigned long long fID){ID=ID;}
                     void SetDay(int fDay){Day=Day;}
                     void SetMonth(int fMonth){Month=Month;}
                     void SetYear(int fYear){Year=Year;}
                     void SetPrice(double fPrice){Price=Price;}
                     string GetName(){return Name;}
                     unsigned long long GetID(){return ID;}
                     int GetDay(){return Day;}
                     int GetMonth(){return Month;}
                     int GetYear(){return Year;}
                     double GetPrice(){return Price;}
                     };
Commodity::Commodity( unsigned long long kID,string kName, int kDay, int kMonth, int kYear, double kPrice)
                     {
                                                 
                                                 ID=kID;
                                                 Name=kName;
                                                 Day=kDay;
                                                 Month=kMonth;
                                                 Year=kYear;
                                                 Price=kPrice;
                                                 }

Thats the class, the whole code of the program is too long to be posted in here. :{
Last edited on
I imagine that Array1 is of type Commodity, meaning your are treating Commodity as a POD type, and it is not. The Name field cannot be written like that, much less read back in.

For your read/write approach to work, you would need to change Name to a fixed-size char array, making Commodity a POD type.

If you don't want to do this (I wouldn't), the solution is more complex. The more straightforward approach would be to write in sequence the ID, Day, Month, Year and Price values, and after that, you write Name.c_str() including the terminating null char.

To simplify the operations, you could define a POD type:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct CommodityData
{
    unsigned long long ID;
    int Day;
    int Month;
    int Year;
    double Price;
};

//And then use that in your Commodity class.
class Commodity
{
private:
    CommodityData Data;
    string Name;
...
};


When you need to read, you would first read sizeof(CommodityData) right into Commodity::Data, and then I would use std::getline() to get the Name string inside Commodity::Name, using '\0' as the terminator char.
Topic archived. No new replies allowed.