How do I continue writing to a file without it starting from scratch?

My code has a function that is able to write lines of data to a file like the following example below:

Format of the .txt file:

Each word is separated by a space.

1
2
3
4
17 35 "door"
40 19 "wall"
17 34 "car"
3  9  "window"



However, the problem is that whenever I exit and later want to resume, the code deletes the previous information and I have to start over again. How do I modify my code to continue from where I left off in the data? My code is list below, however, the print function under the materialList class is what I want to modify.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream> 
using std::cerr;
using std::endl;
#include <list>
using namespace std; 
#include <fstream>
using std::ofstream;
#include <cstdlib> // for exit function
#include <sstream>
using namespace std;

class Item{

//Access specifer
public: //todo, private with get/set 
string item;
int meshNum;
int mNum;

//constructor 
public:
Item( string item,int mNum, int meshNum  ){
    this->item=item;
    this-> mNum= mNum;
    this-> meshNum= meshNum;
    
}

//Memeber functions
public: 
string getItem(){
    return item;
}
void setItem(string item){
    this->item = item;
}
int getMeshNum(){
    return this->meshNum;
}
void setMeshNum(int meshNum){
    this->meshNum= meshNum;
}

int getMNum(){
    return this->mNum;
}
void setMNum(int mNum){
    this-> mNum= mNum;
}

};
//____________________________________________

 class materialList{
// Access specifer
private: 
list <Item> items;
 
 //constructor 
 public:
/* materialList(){
     this->items = new list<Item>;
} */

 // Memeber fucntions
 public:
 void add( const Item& item)
 {
     items.push_back(item);
 }
//print my list
void Print()
 {
     ofstream outdata; // outdata is like cin
     outdata.open("example2.dat"); // opens the file
   if( !outdata ) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }

    for (auto &i : items)
         outdata  << i.getItem() << " "<<i.getMeshNum()<< " "<<i.getMNum()<<endl;  
      
    outdata.close();
 
   
}
void search(ifstream& inFile){
  string line;
  int word;
  int materialNum;
  istringstream iss;
  cout << "Enter a material number:";
  cin >> materialNum;
  int i=0;

  while(!inFile.eof()){
    // read line by line from the file
    getline(inFile,line);
    if(inFile.good()){
      // read word by word in line and place words in arr
      iss.clear(); // clear out state
      iss.str(line);
      iss >> word;
    }

    if (word == materialNum){
      cout << line << endl;
    }

  }
}
    
};
    
Last edited on
When you open the file, use the std::ios::app file mode enum.

'app' stands for 'append'.

ofstream outdata("example2.dat", std::ios::app);
(Note: This declares the variable and opens the file at the same time)
Last edited on
It works, thanks! But, can you explain a little about what "std::ios::app" does?
Last edited on
See http://www.cplusplus.com/reference/fstream/ifstream/open/ for more info about the open modes.
Looking at the code, some of it (eg search) is more complicated than it needs. Consider:

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
#include <iostream>
#include <list>
#include <fstream>
#include <cstdlib> // for exit function
#include <sstream>
using namespace std;

class Item {
private:
	string item;
	int meshNum;
	int mNum;

public:
	Item(const string& item_, int mNum_, int meshNum_) : item(item_), mNum(mNum_), meshNum(meshNum_) {}

	string getItem() const { return item; }
	void setItem(const string& item_) { item = item_; }

	int getMeshNum() const { return meshNum; }
	void setMeshNum(int meshNum_) { meshNum = meshNum_; }

	int getMNum() const { return mNum; }
	void setMNum(int mNum_) { mNum = mNum_; }
};

class materialList {
private:
	list <Item> items;

public:
	void add(const Item& item) { items.push_back(item); }

	void Print()
	{
		ofstream outdata; // outdata is like cout

		outdata.open("example2.dat", std::ios::app); // opens the file for append
		if (!outdata) { // file couldn't be opened
			cerr << "Error: file could not be opened" << endl;
			exit(1);
		}

		for (const auto& i : items)
			outdata << i.getItem() << " " << i.getMeshNum() << " " << i.getMNum() << endl;

		outdata.close();
	}

	void search(ifstream& inFile) {
		int materialNum;

		cout << "Enter a material number:";
		cin >> materialNum;

		for (string line; getline(inFile, line); ) {
			istringstream iss(line);
			int word;

			if (iss >> word && word == materialNum)
				cout << line << endl;

		}
	}

};

Last edited on
Topic archived. No new replies allowed.