#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
bool GetMore();
int main()
{
int Number = 0;
int Input;
ifstream inputstream1; //Read Data
ofstream outputstream1; //Save Data
do
{
inputstream1.open("file.txt");
if (inputstream1.fail() == true)
{
cout << "\nFailed to open file.dat" << endl;
inputstream1.open("file.txt", ios::app);
}
while(inputstream1 >> Input)
{
cout << "The previous number(s) entered was: " << Input << endl;
}
outputstream1.open("file.txt");
while(Number != -1)
{
cout << "\nEnter a number to save to the text file: ";
cin >> Number;
outputstream1 << Number;
outputstream1 << endl;
}
outputstream1.close();
}while(GetMore());
inputstream1.close();
return 0;
}
bool GetMore()
{
char Response;
do
{
cout << "\nDo you want to I/O More? <Y/N> : ";
cin >> Response;
Response = toupper(Response);
}while(Response != 'Y' && Response != 'N');
return (Response == 'Y')? true:false;
}
I am writing a practice program to learn how to use input/output streams outside of school (None of my teachers know how to use I/O streams). My goal for this program is simple:
1. Open file
2. Read from file
3. Overwrite the contents of the file with new integers
4. (Repeat steps 2 and 3 until satisfied)
I have tried initializing another stream to open the file, which allows me to read the data in the file twice (Once for each stream). So I am guessing my problem is that the stream is stuck at EoF, and I do not know how to reset it/send it back to the beginning of the file.
Thanks for the help.
This post has been edited by Mech_1000: 16 June 2008 - 08:11 PM

New Topic/Question
Reply




MultiQuote




|