fstream output and input

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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class inven
{
private:
	string itemDesc,
		date;				//item description
	int quantity;			//quantity
	float wSaleCost;		//whole sale cost
	float retailCost;		//retail cost

public:
	inven();
	void addRecord(fstream&);
};
inven::inven()
{
	itemDesc = "Test";
	date = "Today";
	quantity = 99;
	wSaleCost = 50.00;
	retailCost = 50.00;
}

void inven::addRecord(fstream& File)
{
	string i = "", d ="";
	int q = 0;
	float wsc= 0.0, rc=0.0;
	File >> i >> d >> q >> wsc >> rc;
	cout << i << d << q << wsc << rc << endl;
	cout << itemDesc << "\t" << date << "\t" << quantity << "\t" << wSaleCost << "\t" << retailCost << endl;
	File << itemDesc << '\t' << date << "\t" << quantity << "\t" << wSaleCost << "\t" << retailCost << endl;
}
int main()
{
	fstream File;
	File.open("inventory.txt", ios::in || ios::out || ios::app);
	int choice;

	if (File.fail())	//if File fails to open display error message
		{
		cerr << "Error opening file." << endl;
		return 0;
	}
		inven record;
		record.addRecord(File);
}


Hello! This program is a shortened and isolated issue that I am having with a larger programming homework that I am working on. I'm having issues with outputting the class members into the text file. I'm honestly not sure what the issue is. Also, the program runs fine if I just use ofstream instead of fstream, but in the original program I needed to write and read from a single file. So I am opting for fstream. Thanks guys for reading!

it is this line that I'm talking about
File << itemDesc << '\t' << date << "\t" << quantity << "\t" << wSaleCost << "\t" << retailCost << endl;
Last edited on
I'm honestly not sure what the issue is.
Well, you expect something to happen, but something else happens. You should be able to describe the difference?

One thing is certainly a problem: If the file is empty line 34 will raise an error and you cannot write the data. What is line 34/35 good for anyway?
At a quick glance I see several potential problems.

First your open() call is incorrect, you should be using binary or '|' not the logical operator "||".

Second does your file contain information that can be read for line 34? If not then the stream will enter an error state and no further processing will be possible. You may want to check that the read succeeded.

Third don't forget that you may need to re-position the stream pointer between writes and reads. Remember everything is written to the end of the file when using the app flag.

Last edited on
Thanks for the replies! Lines 34 and 35 is used to test if the text file could be read. It can be read, forgot to mention that in the post, my bad.

Also thank you jib, i'm not sure how I didn't see, but the '|' mistake fixed the issue.
Last edited on
Topic archived. No new replies allowed.