0

The problem i'm having is getting it to save in a regular readable way. All that is ever outputted is garbage non character data.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct inventory {

int id;
int year;
string model;
string trim;
string color;
int engine;

};

int main() {

fstream inFile("inventory.txt", ios::in | ios::binary);
fstream accord("accord6.txt", ios::out | ios::binary);

int id;
int year;
string model;
string trim;
string color;
int engine;
int total = 0;
inventory honda[10], *temp = nullptr;

//copy items into the honda struc array
for (int count = 0; count < 10; count++) {

    inFile >> id >> year >> model >> trim >> color >> engine;

    honda[count].id = id;
    honda[count].year = year;
    honda[count].model = model;
    honda[count].trim = trim;
    honda[count].color = color;
    honda[count].engine = engine;
};

//dynamically allocate a new struc
for (int count = 0; count < 10; count++) {

    if (honda[count].model == "Accord") {
        total += 1;


    }
}
    temp = new inventory[total];
//program is designed to copy all accord details into another text file
    for (int count1 = 0; count1 < total; count1++) {

        for (int count = 0; count < 10; count++) {

            if (honda[count].model == "Accord")
            {
                temp[count1].id = honda[count].id;
                temp[count1].year = honda[count].year;
                temp[count1].model = honda[count].model;
                temp[count1].trim = honda[count].trim;
                temp[count1].color = honda[count].color;
                temp[count1].engine = honda[count].engine;
            }

        }
    }

    //just making sure everything copied correctly
    for (int count1 = 0; count1 < total; count1++) {

        cout << temp[count1].id << endl;
        cout << temp[count1].year << endl;
        cout << temp[count1].model << endl;
        cout << temp[count1].trim << endl;
        cout << temp[count1].color << endl;
        cout << temp[count1].engine << endl;
    }
//this is the problem here when creating the text file. when i open it, its nothing but garbage text. any ideas?
    accord.write(reinterpret_cast<char *>(&temp), sizeof(inventory));


}

And here is the file that i am reading from "inventory.txt":

1001 2014 Civic LX Red 4

1002 2014 Accord LX Blue 4

1005 2014 Accord EX Gold 6

1006 2014 Civic EX Black 4

1007 2014 Civic LX White 4

1010 2015 Accord EX White 6

1011 2015 Accord LX Black 4

1013 2015 Civic EX Red 4

1014 2015 Civic LX Beige 4

1015 2015 Accord EX Beige 6

  • The best way for you to understand your bug is to understand that `sizeof()` is a compile-time constant. The size of the structure is constant, and it always takes the same number of bytes, whether it's strings contain a few characters, or a million characters. – Sam Varshavchik Nov 27 '16 at 00:51
  • Using reinterpret_cast is generally a bad idea. And the structure of your struct is not matching the structures of each line your file. You can also have Unicode issues. After all, your code is completely wrong. Why on earth you assume int fields of your struct match the lines first field? – sergiol Nov 27 '16 at 00:54
  • @sergiol well i'm just following what the book says to do. cant really help if its wrong. but how would i get my structure to match the files contents? – PrestonCoder Nov 27 '16 at 00:58
  • what the book ? – sergiol Nov 27 '16 at 01:09
  • see the duplicate note above your question. – sergiol Nov 27 '16 at 01:10
  • and see what is the encoding of the file you are trying to read. if it is Unicode you need the `w*stream` data types. – sergiol Nov 27 '16 at 01:15
  • @sergiol its "Tony Gaddis Starting Out with C++ from Control Structures to Objects". but try not to get frustrated as this is really only my first semester with c++ and im trying to understand the duplicate answers but struggling as they are more advanced than i am. Also have no idea what serializing is. – PrestonCoder Nov 27 '16 at 01:15
  • I've found the book on https://issuu.com/sheikhuhassan/docs/starting_out_with_c_p1 — what's the page? – sergiol Nov 27 '16 at 01:28
  • unfortunately the link as only till the chapter 6 and I think the code you spoke about is on 12. – sergiol Nov 27 '16 at 01:41
  • @sergiol yeah im in chapter 12 :( – PrestonCoder Nov 27 '16 at 02:30
  • I've found the book on http://cplusplushelp.weebly.com/uploads/2/5/6/5/25655197/0136022537.pdf – sergiol Nov 27 '16 at 20:19

0 Answers0