Saving c++ into a .txt file

Pages: 12
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..
C++ is a text file only.
what you want to do actually its not clear??
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.
oh.. okok..you want to save the order details to some file and later again want to read from that file.. correct???

use fstream to do that. read about streams here:

http://www.cplusplus.com/reference/iostream/

specifically about, ifstream, ofstream and fstream.

the steps which you want are these:
open file in write mode
write the order information to the file
close the file

next again open the file, but this time in read mode
read the information and then close the file.

hope this is what you want!!!??
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Supplies
{
string supplyName;
string supplyAmount;
};



void add ( Supplies & thisSupply );
bool find ( string & supplyName );

void printinventory ( void );



const int items = 4;
Supplies Book [ items ] ;
int arrayIndex;
int Inventory = 0;



int main ( )
{
ofstream SaveFile("Orders.txt");

arrayIndex = 0;
Supplies temp;
char choice;
string supplyName;

do
{
cout << endl << endl << endl;
cout << "Welcome to your lemonade stand!!" << endl;
cout << "What would you like to do?" << endl << endl;
cout << "1. Order supplies" << endl;
cout << "2. Check supplies" << endl;
cout << "3. Check ordered supplies" << endl;
cout << "4. Quit" << endl << endl << endl;

cout << endl << "Enter number...." << endl;


cin >> choice;
while ( choice != '1' && choice != '2' && choice != '3' && choice != '4' )
{
cout << "Bad choice ( 1,2,3 ) , enter choice : ";
cin >> choice;
}
switch ( choice )
{
case '1':
{
cout << endl;
cout << "Enter supply name: ";
cout << endl << "Lemons" << endl;
cout << "Ice" << endl;
cout << "Sugar" << endl;
cout << "Cups" << endl;
cin >> temp.supplyName;
SaveFile << temp.supplyName;
cout << "Enter amount: ";

cin >> temp.supplyAmount;
SaveFile << " "<< temp.supplyAmount;
SaveFile.close();

add ( temp );
};
break;

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?
Last edited on
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.

write all the codes in code blocks, they look readable .
Can you please give me an example? I'm really lost, I don't know too much about this...
The ofstream will not fail if the file is not present or the full path is not given. The files will be created in the program's directory.

For code tags, click the # sign off to the right of the reply box and place your code in between the tags.

C++ is not text only. You can write binary data using C++.
What do you mean remove the # sign??
I mean, when you post your code, Click the # sign that is directly to the right of the reply box.

You get this
[ code ][ / code] // Without spaces

Then post your code like this
[ code ]int main()[ / code]
int main()
Last edited on
How do I open the file in append mode so that everytime I run the program the old orders don't get erased??

case '1':
{

ofstream SaveFile;

SaveFile.open ("Orders.txt");
cout << endl;
cout << "Enter supply name: ";
cout << endl << "Lemons" << endl;
cout << "Ice" << endl;
cout << "Sugar" << endl;
cout << "Cups" << endl;
cin >> temp.supplyName;
SaveFile << temp.supplyName;
cout << "Enter amount: ";
cin >> temp.supplyAmount;
SaveFile << " "<< temp.supplyAmount;



add ( temp );
};
break;


Here's how I input the orders. Do I need to change anything?
To open file for append mode use this:
SaveFile.open("Orders.txt", std::ios::out | std::ios::app);
#include <iostream>
#include <string>
#include <fstream>
#include <fstream.h>

using namespace std;

struct Supplies
{
string supplyName;
string supplyAmount;
};



void add ( Supplies & thisSupply );
bool find ( string & supplyName );

void printinventory ( void );



const int items = 4;
Supplies Book [ items ] ;
int arrayIndex;
int Inventory = 0;



int main ( )
{




arrayIndex = 0;
Supplies temp;
char choice;
string supplyName;

do
{
cout << endl << endl << endl;
cout << "Welcome to your lemonade stand!!" << endl;
cout << "What would you like to do?" << endl << endl;
cout << "1. Order supplies" << endl;
cout << "2. Check supplies" << endl;
cout << "3. Check ordered supplies" << endl;
cout << "4. Delete all ordered supplies" << endl;
cout << "5. Delete an ordered supply" << endl;
cout << "6. Quit" << endl << endl << endl;

cout << endl << "Enter number...." << endl;


cin >> choice;
while ( choice != '1' && choice != '2' && choice != '3' && choice != '4' && choice != '5' && choice != '6' )
{
cout << "Bad choice ( 1,2,3,4,5,6 ) , enter choice : ";
cin >> choice;
}
switch ( choice )
{
case '1':
{

ofstream SaveFile("Orders.txt", ios::app);




cout << endl;
cout << "Enter supply name: ";
cout << endl << "Lemons" << endl;
cout << "Ice" << endl;
cout << "Sugar" << endl;
cout << "Cups" << endl;
cin >> temp.supplyName;
SaveFile << temp.supplyName;
cout << "Enter amount: ";
cin >> temp.supplyAmount;
SaveFile << " "<< temp.supplyAmount;

SaveFile.close();

add ( temp );
};
break;

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 << supplyName <<" is not in the inventory." << endl;
};
break;

case '3':
{


string line;
fstream SaveFile ("Orders.txt");
if (SaveFile.is_open())
{
while (! SaveFile.eof() )
{
getline (SaveFile,line);
cout << line << endl << " ";
}
SaveFile.close();
}

else cout << "Unable to open file";





};
break;

case '4':




{
ofstream SaveFile("Orders.txt", ios::trunc);
SaveFile.close();
}


break;









};
}
while ( choice != '6' );

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;
}




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?
Last edited on
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.

Last edited on
Can you please make a sample code for me sir? Cause I don't know anything about this problem...
File read:
1
2
3
4
5
6
7
8
9
10
11
12
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 
Thank you very much sir, I'll try your codes once I get home.. ^^
If they don't work, post the error message because I didn't test these.
Where do I put these codes sir?
By the way, do I still need to edit these?
Last edited on
Pages: 12