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