seekg

I am trying to read data from a file one line at a time. The file was originally written by this same program using \n to separate data. It seems I should use seekg to move to the next piece of data, but I'm not sure how to implement it.

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct Item
{
	string ItemName;
	string BrandName;
	string UnitLabel;
	int Department;
	int Location;
	float UnitCost;
	Item* NextItem;
	Item* PreviousItem;
};

class ItemList
{
private:
	Item* FirstItem;
	int ListLength;

public:
	ItemList()
	{
		FirstItem = NULL;
		ListLength = 0;
	}
	void AddItem(string IN, string BN, string UL, int D, int L, float UC)
	{
		Item* NewItem = new Item;
		NewItem->ItemName = IN;
		NewItem->BrandName = BN;
		NewItem->UnitLabel = UL;
		NewItem->Department = D;
		NewItem->Location = L;
		NewItem->UnitCost = UC;
		NewItem->NextItem = FirstItem;
		FirstItem = NewItem;
		ListLength++;
	}
	bool ListOccupied()
	{
		return (FirstItem != NULL);
	}
	int Length()
	{
		return ListLength;
	}
	Item* FirstItemLoc()
	{
		return FirstItem;
	}
	void DisplayAllItemList(int i)
	{
		Item* CurrentItem = FirstItem;
		int ItemCount = 0;
		while(CurrentItem!=NULL)
		{
			if(ItemCount == i)
			{
				cout << CurrentItem->ItemName << "\t"
					 << CurrentItem->BrandName << "\t"
					 << CurrentItem->UnitCost << "\t"
					 << CurrentItem->UnitLabel << "\t"
					 << CurrentItem->Department << "\t"
					 << CurrentItem->Location << endl;
				CurrentItem = CurrentItem->NextItem;
				break;
			}
			ItemCount++;
			CurrentItem = CurrentItem->NextItem;
		}
	}
};

void AddItem(string&,string&,string&,int&,int&,float&);
bool MoreItems();
bool Menu(int&);
bool MoreLocations();
void SaveAllItemList(int,Item*);
void LoadAllItemList(string&,string&,string&,int&,int&,float&,ItemList);

int main()
{
	ItemList AllItems;
	string ItemName;
	string BrandName;
	string UnitLabel;
	int Department;
	int Location;
	float UnitCost;
	int CurrentTask=0;

	while(Menu(CurrentTask))
	{
		switch(CurrentTask)
		{
		case 0: 
		case 1: do
				{
					AddItem(ItemName,BrandName,UnitLabel,Department,Location,UnitCost);
					AllItems.AddItem(ItemName,BrandName,UnitLabel,Department,Location,UnitCost);
				}while(MoreItems());
				break;
		case 2: do
				{

				}while(MoreLocations());
				break;
		case 3: SaveAllItemList(AllItems.Length(),AllItems.FirstItemLoc());
				break;
		case 4: {  // Here's where I need help.
					int ListLength;
					ifstream inData;
					inData.open("list.out");
					inData >> ListLength;
					for(int i=0;i<ListLength;i++)
					{
						
						inData.seekg(2,ios_base::cur);
						getline(inData,ItemName);
						inData.seekg(1,ios_base::cur);
						getline(inData,BrandName);
						inData.seekg(1,ios_base::cur);
						inData >> UnitCost;
						inData.seekg(1,ios_base::cur);
						getline(inData,UnitLabel);
						inData.seekg(1,ios_base::cur);
						inData >> Department;
						inData.seekg(1,ios_base::cur);
						inData >> Location;
						AllItems.AddItem(ItemName,BrandName,UnitLabel,Department,Location,UnitCost);
					}
					inData.close();
					break;
				}
		case 5: for(int i=0;i<AllItems.Length();i++) AllItems.DisplayAllItemList(i);
				system("PAUSE");
		}
	}

	return 0;
}
void AddItem(string& IN, string& BN, string& UL, int& D, int& L, float& UC)
{
	system("cls");
	cout << "Enter data for new item below.\n\n"
		 << "\tItem Name:     ";
	cin.ignore();
	getline(cin,IN);
	cout << "\tBrand Name:    ";
	getline(cin,BN);
	cout << "\tUnit Cost:     ";
	cin >> UC;
	cout << "\tUnit Label:    ";
	cin.ignore();
	getline(cin,UL);
	cout << "\tDepartment:    ";
	cin >> D;
	cout << "\tLocation:      ";
	cin >> L;
}
bool MoreItems()
{
	char UserReply;
	do{
		cout << "Are there more items to enter? (Y/N) -->  ";
		cin >> UserReply;
		UserReply=toupper(UserReply);
		if(UserReply!='Y'&&UserReply!='N') cout << "*** Invalid response *** \n";
	}while(UserReply!='Y'&&UserReply!='N');
	return (UserReply=='Y');
}
bool Menu(int& CurrentTask)
{
	int UserReply;
	do
	{
		system("cls");
		cout << "What would you like to do?\n"
			 << "\t0)  Quit.\n"
			 << "\t1)  Add new item.\n"
			 << "\t2)  Add new location.\n"
			 << "\t3)  Save All Item list.\n"
			 << "\t4)  Load All Item list.\n"
			 << "\t5)  Display All Item list.\n\n"
			 << "Enter response here:  ";
		cin >> UserReply;
		if(UserReply<0&&UserReply>5)
		{
			cout << "*** Invalid Response ***";
			system("PAUSE");
		}
	}while(UserReply<0&&UserReply<5);
	if(UserReply==0) return false;
	else
	{
		CurrentTask = UserReply;
		return true;
	}
}
bool MoreLocations()
{
	char UserReply;
	do{
		cout << "Are there more locations to enter? (Y/N) -->  ";
		cin >> UserReply;
		UserReply=toupper(UserReply);
		if(UserReply!='Y'&&UserReply!='N') cout << "*** Invalid response *** \n";
	}while(UserReply!='Y'&&UserReply!='N');
	return (UserReply=='Y');
}
void SaveAllItemList(int ListLength, Item* FirstItem)
{
	ofstream outData;
	outData.open("list.out");
	outData << ListLength << "\n\n";
	for(int i=0;i<ListLength;i++)
	{
		Item* CurrentItem = FirstItem;
		int ItemCount = 0;
		while(CurrentItem!=NULL)
			{
				if(ItemCount == i)
				{
					outData << CurrentItem->ItemName << "\n"
							<< CurrentItem->BrandName << "\n"
							<< CurrentItem->UnitCost << "\n"
							<< CurrentItem->UnitLabel << "\n"
							<< CurrentItem->Department << "\n"
							<< CurrentItem->Location << "\n\n";
					break;
				}
				ItemCount++;
				CurrentItem = CurrentItem->NextItem;
			}
	}
	outData.close();

}


Here is the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2

Bread
Sunbeam
1.99
unit
3
2

Milk
asdf
2.99
unit
3
1
Last edited on
Either get each line with
1
2
3
4
5
6
7
8
  std::string line;

  // read the line
  getline( inData, line );

  // convert it to what I want
  std::stringstream ss( line );
  ss >> CurrentItem->UnitCost;


Or ignore everything else on the line after reading your datum:
1
2
  inData >> CurrentItem->UnitCost;
  inData.ignore( numeric_limits<streamsize>::max(), '\n' );


Hope this helps.
Won't your suggestion read each line as a string? Although several of the lines are meant to be handled as strings, I also need a float and two integers to remain their respective data types.
In the file, _all_ your data is a string. The only difference between the two methods I posted is which stream you use to convert your data back into a float (as per the examples) or integer.
I see your point about each line being a string. I'm embarrased I didn't realize that on my own. I couldn't figure out how to use the code you provided but you did lead me in the right direction. I used atof() and atoi() and it works perfectly. Thank you for the help.

p.s. - If you have any suggestions on how to clean this code up any I would appreciate it. Thanks again.

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct Item
{
	string ItemName;
	string BrandName;
	string UnitLabel;
	int Department;
	int Location;
	float UnitCost;
	Item* NextItem;
};

class ItemList
{
private:
	Item* FirstItem;
	int ListLength;

public:
	ItemList()
	{
		FirstItem = NULL;
		ListLength = 0;
	}
	void AddItem(string IN, string BN, string UL, int D, int L, float UC)
	{
		Item* NewItem = new Item;
		NewItem->ItemName = IN;
		NewItem->BrandName = BN;
		NewItem->UnitLabel = UL;
		NewItem->Department = D;
		NewItem->Location = L;
		NewItem->UnitCost = UC;
		NewItem->NextItem = FirstItem;
		FirstItem = NewItem;
		ListLength++;
	}
	bool ListOccupied()
	{
		return (FirstItem != NULL);
	}
	int Length()
	{
		return ListLength;
	}
	Item* FirstItemLoc()
	{
		return FirstItem;
	}
	void DisplayAllItemList(int i)
	{
		Item* CurrentItem = FirstItem;
		int ItemCount = 0;
		while(CurrentItem!=NULL)
		{
			if(ItemCount == i)
			{
				cout << CurrentItem->ItemName << "\t"
					 << CurrentItem->BrandName << "\t"
					 << CurrentItem->UnitCost << "\t"
					 << CurrentItem->UnitLabel << "\t"
					 << CurrentItem->Department << "\t"
					 << CurrentItem->Location << endl;
				CurrentItem = CurrentItem->NextItem;
				break;
			}
			ItemCount++;
			CurrentItem = CurrentItem->NextItem;
		}
	}
	void SaveItemList()
	{
		ofstream outData;
		outData.open("list.out");
		for(int i=0;i<ListLength;i++)
		{
			Item* CurrentItem = FirstItem;
			int ItemCount = 0;
			while(CurrentItem!=NULL)
				{
					if(ItemCount == i)
					{
						outData << CurrentItem->ItemName << "\n"
								<< CurrentItem->BrandName << "\n"
								<< CurrentItem->UnitCost << "\n"
								<< CurrentItem->UnitLabel << "\n"
								<< CurrentItem->Department << "\n"
								<< CurrentItem->Location;
						if(CurrentItem!=NULL) cout << "\n\n";
						break;
					}
					ItemCount++;
					CurrentItem = CurrentItem->NextItem;
				}
		}
		outData.close();
	}
	void LoadItemList()
	{
			ifstream inData;
			inData.open("list.out");
			while(!inData.eof())
			{
				string line;
				Item* NewItem = new Item;
				
				getline(inData,NewItem->ItemName);
				getline(inData,NewItem->BrandName);
				getline(inData,line);
				NewItem->UnitCost = atof(line.c_str());
				getline(inData,NewItem->UnitLabel);
				getline(inData,line);
				NewItem->Department = atoi(line.c_str());
				getline(inData,line);
				NewItem->Location = atoi(line.c_str());
				getline(inData,line);

				NewItem->NextItem = FirstItem;
				FirstItem = NewItem;
				ListLength++;

			}
			inData.close();
	}
};

void AddItem(string&,string&,string&,int&,int&,float&);
bool MoreItems();
bool Menu(int&);
bool MoreLocations();
void SaveAllItemList(int,Item*);
void LoadAllItemList(string&,string&,string&,int&,int&,float&,ItemList);

int main()
{
	ItemList AllItems;
	string ItemName;
	string BrandName;
	string UnitLabel;
	int Department;
	int Location;
	float UnitCost;
	int CurrentTask=0;

	while(Menu(CurrentTask))
	{
		switch(CurrentTask)
		{
		case 0: 
		case 1: do
				{
					AddItem(ItemName,BrandName,UnitLabel,Department,Location,UnitCost);
					AllItems.AddItem(ItemName,BrandName,UnitLabel,Department,Location,UnitCost);
				}while(MoreItems());
				break;
		case 2: do
				{

				}while(MoreLocations());
				break;
		case 3: AllItems.SaveItemList();
				break;
		case 4: AllItems.LoadItemList();
				break;
		case 5: for(int i=0;i<AllItems.Length();i++) AllItems.DisplayAllItemList(i);
				cout << "\nListLength = " << AllItems.Length() << "\n\n";
				system("PAUSE");
		}
	}

	return 0;
}
void AddItem(string& IN, string& BN, string& UL, int& D, int& L, float& UC)
{
	system("cls");
	cout << "Enter data for new item below.\n\n"
		 << "\tItem Name:     ";
	cin.ignore();
	getline(cin,IN);
	cout << "\tBrand Name:    ";
	getline(cin,BN);
	cout << "\tUnit Cost:     ";
	cin >> UC;
	cout << "\tUnit Label:    ";
	cin.ignore();
	getline(cin,UL);
	cout << "\tDepartment:    ";
	cin >> D;
	cout << "\tLocation:      ";
	cin >> L;
}
bool MoreItems()
{
	char UserReply;
	do{
		cout << "Are there more items to enter? (Y/N) -->  ";
		cin >> UserReply;
		UserReply=toupper(UserReply);
		if(UserReply!='Y'&&UserReply!='N') cout << "*** Invalid response *** \n";
	}while(UserReply!='Y'&&UserReply!='N');
	return (UserReply=='Y');
}
bool Menu(int& CurrentTask)
{
	int UserReply;
	do
	{
		system("cls");
		cout << "What would you like to do?\n"
			 << "\t0)  Quit.\n"
			 << "\t1)  Add new item.\n"
			 << "\t2)  Add new location.\n"
			 << "\t3)  Save All Item list.\n"
			 << "\t4)  Load All Item list.\n"
			 << "\t5)  Display All Item list.\n\n"
			 << "Enter response here:  ";
		cin >> UserReply;
		if(UserReply<0&&UserReply>5)
		{
			cout << "*** Invalid Response ***";
			system("PAUSE");
		}
	}while(UserReply<0&&UserReply<5);
	if(UserReply==0) return false;
	else
	{
		CurrentTask = UserReply;
		return true;
	}
}
bool MoreLocations()
{
	char UserReply;
	do{
		cout << "Are there more locations to enter? (Y/N) -->  ";
		cin >> UserReply;
		UserReply=toupper(UserReply);
		if(UserReply!='Y'&&UserReply!='N') cout << "*** Invalid response *** \n";
	}while(UserReply!='Y'&&UserReply!='N');
	return (UserReply=='Y');
}
Last edited on
Topic archived. No new replies allowed.