Unable to get size of vector / store data from struct vector into textfile

I managed to read data from the textfile that uses `:` as a delimter. I'm able to get the data from the text file, store into the struct and vector and obtain the size as shown below.

int InventoryDatabase::readToArray (const char* filename) {

fstream afile;

afile.open (filename, ios::in);

entry e;

while(getline(afile, tempstring))
{
string tempID = tempstring.substr(0,tempstring.find(delimiter));
tempstring.erase(0, tempstring.find(delimiter) + delimiter.length());
e.stockID = ( strToNum(tempID) );

string tempItem = tempstring.substr(0,tempstring.find(delimiter));
tempstring.erase(0, tempstring.find(delimiter) + delimiter.length());
e.stockItem = ( tempItem );

vectorOfEntry.push_back(e);

}

afile.close();

return vectorOfEntry.size(); // return 350
}

I tried to get the size of the vector , it return me 0. It also doesn't store the data in the vector into the textfile

int InventoryDatabase::writeToFile (const char* filename){ //instance of the class InventoryDatabase

fstream afile;

afile.open(filename, ios::out | ios::app);

int size = vectorOfEntry.size(); // extract number of struct in vector

for (int i = 0; i < size; i++) // data unable to store into text file
{
afile << vectorOfEntry[i].stockID
<< ":"
<< vectorOfEntry[i].stockItem;
}

afile.close();

return size; // return 0
}

This is how i store the struct of vector in my class file. my struct is stored globally while my vector is stored in a protected sector.

struct entry //example of some data of the struct
{
int stockID;
string stockItem;
};

class InventoryDatabase {
public:
InventoryDatabase();

int writeToFile (const char* filename);

protected:
vector<entry> vectorOfEntry;
};


This is my main function where i will use the `writeToFile` function to write the necessary data into the textfile

int main()
{
InventoryDatabase inventDB;
int writeSize, readSize;

readSize = inventDB.readToArray("SampleData.txt");
cout << "Successful reading! Size is : " << readSize << endl; // display 350

writeSize = inventDB.writeToFile("writeFile.txt");
cout << "Successful writing ! Size is : " << writeSize << endl; // displays 0
}
Last edited on
I am not sure where the problem is since you didn't show us the whole code. In order to compile I added the missing pieces. Have a look if it works for you:
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct entry //example of some data of the struct
{
  int stockID;
  string stockItem;
};

int strToNum(string s)
{
  return stoi(s);
}

class InventoryDatabase {
public:
  InventoryDatabase();

  int writeToFile(const char* filename);
  int readToArray(const char* filename);
protected:
  vector<entry> vectorOfEntry;
};

InventoryDatabase::InventoryDatabase()
{

}

int InventoryDatabase::readToArray(const char* filename) 
{
  fstream afile;
  string tempstring;
  string delimiter = ":";
  afile.open(filename, ios::in);

  entry e;

  while (getline(afile, tempstring))
  {
    string tempID = tempstring.substr(0, tempstring.find(delimiter));
    tempstring.erase(0, tempstring.find(delimiter) + delimiter.length());
    e.stockID = (strToNum(tempID));

    string tempItem = tempstring.substr(0, tempstring.find(delimiter));
    tempstring.erase(0, tempstring.find(delimiter) + delimiter.length());
    e.stockItem = (tempItem);

    vectorOfEntry.push_back(e);

  }

  afile.close();

  return vectorOfEntry.size(); // return 350
}
int InventoryDatabase::writeToFile(const char* filename) 
{ 
  fstream afile;

  afile.open(filename, ios::out | ios::app);
  if (!afile)
  {
    perror("Error creating file");
    return -1;
  }
  int size = vectorOfEntry.size(); // extract number of struct in vector

  for (int i = 0; i < size; i++) // data unable to store into text file
  {
    afile << vectorOfEntry[i].stockID
      << ":"
      << vectorOfEntry[i].stockItem << endl;
  }

  afile.close();

  return size; // return 0
}

using namespace std;

int main(void)
{
  InventoryDatabase inventDB;
  int writeSize, readSize;

  readSize = inventDB.readToArray("SampleData.txt");
  cout << "Successful reading! Size is : " << readSize << endl; // display 350

  writeSize = inventDB.writeToFile("writeFile.txt");
  cout << "Successful writing ! Size is : " << writeSize << endl; // displays 0
  
  system("pause");
  return 0;
}


Input file I created:
1: Hammer
2: Zange
3: Saege
4: Naegel

Output:

Successful reading! Size is : 4
Successful writing ! Size is : 4
Press any key to continue . . .
Topic archived. No new replies allowed.