Can anyone explain the easiest way to save a file?? And to load it as well... Or can anyone point me to a tutorial about this?? Thanks, I'll really appreciate it..
I'm making a program that manages and orders supplies of Pencils.
I've already done the other codes, but I don't know how to save the amount of Pencils that the user wants to be delivered, the user is also the one who inputs the amount.
case '2':
{
cout << "Enter supply name: ";
cout << endl << "Lemons" << endl;
cout << "Ice" << endl;
cout << "Sugar" << endl;
cout << "Cups" << endl;
cin >> supplyName;
if ( find ( supplyName ) )
cout << supplyName << " is in the inventory." << endl;
else
cout << "name is not in the inventory." << endl;
};
break;
case '3':
{
printinventory ( );
};
break;
};
}
while ( choice != '4' );
return 0 ;
}
//FUNCTIONS.....
void add ( Supplies & thisSupply )
{
if ( arrayIndex >= items )
arrayIndex = 0;
if ( Inventory < items )
Inventory++;
Book [ arrayIndex ].supplyName = thisSupply.supplyName;
Book [ arrayIndex ].supplyAmount = thisSupply.supplyAmount;
arrayIndex++;
return;
}
//
bool find( string & supplyName )
{
int i = 0;
while ( i < Inventory )
{
if ( supplyName == Book [ i ].supplyName )
return true;
i++;
}
return false;
}
//
void printinventory ( void )
{
int i = 0;
while ( i < Inventory && i < items )
{
cout << i << ". " << Book [ i ].supplyName<< " " << Book [ i ].supplyAmount << endl;
i++;
}
return;
}
So far, the saving works, but it only saves 1 supply that the user inputs.. How can I make this save more than 1 supply?? Also, how or what codes should I use to get the text written on the .txt file?
1. give full path of file here otherwise it may fail anytime.
ofstream SaveFile("Orders.txt");
2. check the default behaviour of ofstream constructor, if its opening in write mode then what will happen is that, on each open your last instance will be wiped of. so open in append mode. in append mode you can again and again open the file, write to it and close it. each write will be written to the end of the file.
3. to read you can use
ifstream.
open the same way as you did for write.
then
ifstream::read or ifstream::get_line
will get you lines what you have written.
keep reading till not end of file like this: while(!ifstream::eof())
when you have read all the data close the stream. thats it.
Book [ arrayIndex ].supplyName = thisSupply.supplyName;
Book [ arrayIndex ].supplyAmount = thisSupply.supplyAmount;
arrayIndex++;
return;
}
//
bool find( string & supplyName )
{
int i = 0;
while ( i < Inventory )
{
if ( supplyName == Book [ i ].supplyName )
return true;
i++;
}
return false;
}
//
void printinventory ( void )
{
int i = 0;
while ( i < Inventory && i < items )
{
cout << i << ". " << Book [ i ].supplyName<< " " << Book [ i ].supplyAmount << endl;
i++;
}
return;
}
Iv'e coded this so far, and I was able to open the file in append mode, thank you very much for the replies ^^
Now, I wanted to add a new feature for the program, as you can see I've added also a feature where the user can delete all his ordered items, but the problem is, it deletes all his orders, I want a feature where he can just choose to delete 1 item instead of everything.
Is this possible?
Yes it is. I'm not very familiar with searching and replacing in text files.
seekg, and seekp might be used.
You could also read in the file and store it into a vector or some sort of a container. Then use the find and erase functions and write the new data back to the file.
std::vector<std::string> file;
std::string temp;
ifstream infile("Orders.txt");
while( !infile.eof() )
{
getline(infile, temp);
file.push_back(temp);
}
// the file vector now holds the entire file
File Search:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//I'm assuming you will place these code fragments in the correct place
// and that the file vector is already ready to use.
std::string item;
cout << "Enter an item to delete from the order: ";
getline(cin, item);
for(int i = 0; i < file.size(); ++i)
{
if(file[i] == item)
{
file.erase(file.begin() + i);
cout << "Order erased at line " << i+1 << endl;
}
}
// write the vector back to file