I am working on an OOP project and I need to write into a file and I faced a problem that each time I do it the file is over written with only one object. How can I make it to write data of all the objects? I tried this but didnt work.
virtual void save(ofstream outfile) = 0;`// the base class
void AND2::save(ofstream outf) //derived
{
outf.open("test.txt");
outf << Component::getype() << " ";
outf<< Component::getid() << " ";
outf << Component:: graphicsinfomration().x1 << " ";
outf<< Component::graphicsinfomration().x2 << " ";
outf << graphicsinfomration().y1 << " ";
outf << graphicsinfomration().y2 << " ";
outf << endl;
outf.close();
}
else
{
ofstream outf;
for (int i = 0; i < (pApp->getcompcount()); i++)
{
//ask user to enter text name
c[i]->save( outf);
}
pOut->ClearStatusBar();
}
Because you're opening the file again and again you overwrite the contents again and again.
You probably want to open the stream outside the for loop once and pass it by reference.
Related
this is a programme that needs to save the outputs to a user specified data file however it doesn't seem to save and Im not sure why, Im relatively new to C++ so any help is appreciated
cout << "Press 's' then 'Enter' to save the file or any other key then 'Enter' to display";
cin >> save;
if (save != 's')
{
cout << "Ix = " << Ix << "A\n";
cout << "Iy = " << Iy << "A\n";
cout << "Vz = " << Vz << "V\n";
}
else
{
cout << "Please enter a name for your file: \n";
cin >> filename;
cout << " Please enter a directory to save your file in: \n";
cin >> filepath;
ofstream file((filepath + "/" + filename).c_str());
//input is being writen to the file
file << "Ix = " << Ix << "A\n";
file << "Iy = " << Iy << "A\n";
file << "Vz = " << Vz << "V\n";
file << flush;
file.close();
}
}
welcome to SO.
When opening a file stream you first have to check whatever the opening operation has succeeded.
You could do it like this:
if(!file) { /* file isn't "good", open seems to have failed */}
/* or */
if(!file.good()) { /* file isn't good */ }
I guess, because its not writing anything to the file (nor creating the file?) the directory probably does not exists.
The std::ofstream class will not automatically create the required directories.
How you could create the required directories is well explained here: https://en.cppreference.com/w/cpp/filesystem/create_directory
I have an infinite loop while trying to read a file. File is saved from user input, then it is to be read, finally, displayed using a separate function.
Here is my read function. The loop is not finding the eof(); from the file. I can't see what the issue is. No compiler errors.
void read(HouseholdItems items[AMOUNT], fstream& myFile, fstream& yourFile)
{
myFile.open("insured.dat", ios::in | ios::binary); // Open myFile
yourFile.open("uninsured.dat", ios::in | ios::binary); // Open yourFile
for(int i = 0; i < AMOUNT; i++)
{
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
for(i = 0; i < AMOUNT; i++)
{
while (myFile)
{
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: ";
cout << items[i].insured << endl;
}
}
}
myFile.close();
yourFile.close();
}
The following loop can never end : no modification is made to myFile inside its body :
while (myFile)
{
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: ";
cout << items[i].insured << endl;
}
You have to read the file inside the while loop for it to end.
Besides, you most likely have a variable name conflict: you have two for loops that use the same variable i ; which is presumably not what you want.
Your problem is while(myFile), since nothing about myFile changes in that loop. It isn't clear what it is you mean for that loop to accomplish, so I can't say what to replace it with. (It does seem problematic that you have nested for loops, but don't seem to have a table of data.)
The answers by #ScottHunter and #Ekelog already answer the real problem. The following are peripheral problems.
These lines are not right:
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
You need to use:
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
// ^^ Drop the &
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
// ^^ Drop the &
try this in while
while (!myFile.eof())
{
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: ";
cout << items[i].insured << endl;
}
See inline comments:
void read(HouseholdItems items[AMOUNT], fstream& myFile, fstream& yourFile)
{
// since you open and close those files here, you probably
// want to declare them here instead as a function parameter
myFile.open("insured.dat", ios::in | ios::binary); // Open myFile
yourFile.open("uninsured.dat", ios::in | ios::binary); // Open yourFile
// test in the loop
for(int i = 0; i < AMOUNT && myFile && yourFile; i++)
{
// these two reads do not make sense, the second one
// will overwrite the data just read by the first one...
// maybe you meant that one of the file might be smaller?
// or maybe to compare the results in some ways (in which
// case you need two arrays)
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
// write current result
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: ";
cout << items[i].insured << endl;
}
myFile.close();
yourFile.close();
}
As a side note:
Your second for() loop reused the i variable which means it would not work at all as expected.
As pointed out by others the sizeof() was wrong, you could also use sizeof(items[0]) since all items are equal in size.
As mentioned by SamIAm, the while() was blocking because the file was not being read so the EOF never actually reached in the file.
As shown in the comments, the file objects should probably be defined in the function instead of outside and passed in as references.
I am about to be finished with a program I am writing and have reached a roadblock.
I am trying to print the contents of a function called print which is called by a pointer.
My problem is I need to print the contents of the function to an output file and am not sure how.
This is my print function:
void English::Print(){
int formatlength = 38 - (static_cast<int>(firstName.size() + lastName.size()));
cout << firstName << " " << lastName;
cout << setw(formatlength) << finalExam;
cout << setprecision(2) << fixed << setw(11) << FinalGrade();
cout << setw(4) << Lettergrade() << endl;
}
This is the implementation of the print function:
for (int i = 0; i <= numStudents - 1; i++) {
if (list[i]->GetSubject() == "English") {
list[i]->Print();
}
}
Where the for loop is cycling through my list of Students.
My goal is that the list[i]->Print() will print to my output file.
Simply replace cout with an ostream object, something like :
void English::Print(ostream& fout){
//ofstream of("myfile.txt", std::ios_base::app);
int formatlength = 38 - (static_cast<int>(firstName.size() + lastName.size()));
fout << firstName << " " << lastName;
fout << setw(formatlength) << finalExam;
fout << setprecision(2) << fixed << setw(11) << FinalGrade();
fout << setw(4) << Lettergrade() << endl;
}
Also, you can overload << operator too in your class English
friend ostream& operator <<( ostream& os, const English& E )
{
//
return os;
}
And then can simply use:
fout << list[i] ;
Besides the answers above, I think you should try this way, using the C's original file redirection function:
Put this instruction in the first line of your main function:
int main(){
freopen("out.txt", "w", stdout);
//your codes
The "out.txt" is the file you wanna to put the data in, "w" means you want to write in the file, and stdout is the standard output stream that has been redirected.
I've tried to write a simple database program. The problem is that ofstream does NOT want to make a new file.
Here's an extract from the offending code.
void newd()
{
string name, extension, location, fname;
cout << "Input the filename for the new database (no extension, and no backslashes)." << endl << "> ";
getline(cin, name);
cout << endl << "The extension (no dot). If no extension is added, the default is .cla ." << endl << "> ";
getline(cin, extension);
cout << endl << "The full directory (double backslashes). Enter q to quit." << endl << "Also, just fyi, this will overwrite any files that are already there." << endl << "> ";
getline(cin, location);
cout << endl;
if (extension == "")
{
extension = "cla";
}
if (location == "q")
{
}
else
{
fname = location + name + "." + extension;
cout << fname << endl;
ofstream writeDB(fname);
int n = 1; //setting a throwaway inteher
string tmpField, tmpEntry; //temp variable for newest field, entry
for(;;)
{
cout << "Input the name of the " << n << "th field. If you don't want any more, press enter." << endl;
getline(cin, tmpField);
if (tmpField == "")
{
break;
}
n++;
writeDB << tmpField << ": |";
int j = 1; //another one
for (;;)
{
cout << "Enter the name of the " << j++ << "th entry for " << tmpField << "." << endl << "If you don't want any more, press enter." << endl;
getline(cin, tmpEntry);
if (tmpEntry == "")
{
break;
}
writeDB << " " << tmpEntry << " |";
}
writeDB << "¬";
}
cout << "Finished writing database. If you want to edit it, open it." << endl;
}
}
EDIT: OK, just tried
#include <fstream>
using namespace std;
int main()
{
ofstream writeDB ("C:\\test.cla");
writeDB << "test";
writeDB.close();
return 0;
}
and that didn't work, so it is access permission problems.
ofstream writeDB(fname); //-> replace fname with fname.c_str()
If you lookup the documentation of the ofstream constructor, you will see something like:
explicit ofstream ( const char * filename, ios_base::openmode mode = ios_base::out );
The second argument is optional, but the first one is a const char*, and not a string. To solve this problem the most simple way is to convert your string to something called a C-string (char*, which is basically an array of chars); to do that just use c_str() (it«s part of the library).
Other than that, you could just place the information directly on a C-str, and then pass it normally to the ofstream constructor.
I want to write a little program which should be used in supermarkets. everything is fictitious and it's only for learning purposes.
However, The tool generate a new data for every new article. in the data there are 2 lines, the name and the prise.
The data is named as the article number of the product. So the user enter a articlenumber and the tool looks for a data with this number, if it found it, it reads the 2 lines and initiates the variables.
But for some reasons it does not convert and copy the strings correctly.
here is the part which loads the data.
int ware::load()
{
string inhalt;
cout << "please insert article number" << endl;
cin >> articlenumber;
productname.open(articlenumber, ios::in);
if (!productname.is_open())
{
cout << "can't find the product." << endl;
return 1;
}
if (productname.is_open())
{
while (!productname.eof())
{
getline(productname, inhalt);
strcpy(name,inhalt.c_str());
getline(productname, inhalt);
price = atoi (inhalt.c_str());
cout << inhalt << endl;
}
warenname.close();
}
cout << endl << endl <<
"number: " << inhalt <<
" preis: " << price <<
" name: " << name <<
endl << endl; //this is a test and will be deleted in the final
}
hope you can help me!
Here is the class:
class ware{
private:
char articlenumber[9];
char name[20];
int price;
fstream warennamefstream;
ifstream warenname;
public:
void newarticle(); //this to make a new product.
void scan(); //this to 'scan' a product (entering the article number ;D)
void output(); //later to output a bill
int load(); //load the datas.
};
hope everything is fine now.
First, you have a using namespace std; somewhere in your code. This occasionally leads to subtle bugs. Delete it. ( Using std Namespace )
int ware::load()
{
string inhalt;
cout << "please insert article number" << endl;
cin >> articlenumber;
The type of articlenumber is incorrect. Declare it std::string, not char[]. ( What is a buffer overflow and how do I cause one? )
productname.open(articlenumber, ios::in);
There is no reason to have an ifstream lying around waiting to be used. Also, there is no point in providing ios::in -- it is the default. Just use the one-argument form of the ifstream constructor.
if (!productname.is_open())
{
cout << "can't find the product." << endl;
return 1;
}
Don't bother checking to see if the file opened. Your users don't care if the file was present or not, they care whether the file was present AND you retrieved the essential data.
if (productname.is_open())
{
while (!productname.eof())
{
getline(productname, inhalt);
strcpy(name,inhalt.c_str());
getline(productname, inhalt);
price = atoi (inhalt.c_str());
cout << inhalt << endl;
}
warenname.close();
}
This loop is just wrong.
Never invoke eof(). It doesn't do what you think it does, and will cause bugs.
Why is this a loop? Aren't there only two lines in the file?
There is no point in calling close. Just let the file close when the istream goes out of scope.
Why is warename different than productname?
Don't store your data in char[]. This is the 21st century. Use std::string.
.
cout << endl << endl <<
"number: " << inhalt <<
" preis: " << price <<
" name: " << name <<
endl << endl; //this is a test and will be deleted in the final
Never use endl when you mean to say '\n'. Each of those endl manipulators invokes flush, which can be very expensive. ( What is the C++ iostream endl fiasco? )
You forgot to return a value.
Try this instead:
int ware::load()
{
// This declaration should be local
std::string articlenumber;
cout << "please insert article number" << endl;
cin >> articlenumber;
// This declaration should be local
std::ifstream productname(articlenumber.c_str());
// These declarations can be class members:
std::string name;
int price;
std::string number;
if(getline(productname, name) &&
productname>>price &&
productname>>number)
{
cout << "\n\n" <<
"number: " number <<
" preis: " << price <<
" name: " << name <<
"\n\n"; //this is a test and will be deleted in the final
return 0;
} else {
cout << "can't find the product." << endl;
return 1;
}
}