Writing a dynamically allocated struc to a txt file

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.

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
75
76
77
78
79
80
81
82
83
  #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));
Split your code up into separate functions to make main() cleaner and easier to read.

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
// don't use magic numbers
const int INVENTORY_SIZE{ 10 };

void read_inventory( const string& filename, 
                    inventory inv[INVENTORY_SIZE] )
{
    ifstream ifs{ filename };
    if( !ifs ) {
        cout << "could not open " + filename + '\n';
        return;
    }

    for( int i{}; i < INVENTORY_SIZE; i++ ) {
        ifs >> inv[i].id 
            >> inv[i].year 
            >> inv[i].model 
            >> inv[i].trim 
            >> inv[i].color 
            >> inv[i].engine;

        if( ifs.fail( ) ) {
            cout << "bad formatting of " + filename + '\n';
            return;
        }
    }
}


1
2
//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));

Consider this:
1
2
const string str{ "Hello World" };
cout << reinterpret_cast<const char*>( &str ) << "\n";
<�Z
Last edited on
ok thats great. now how do I format the out put? im doing accord << honda[x].x << endl;
but the for outputted file is just one long continuous string?
Topic archived. No new replies allowed.