Input/Output Stream Issue

Reading a file multiple times

Page 1 of 1

5 Replies - 1453 Views - Last Post: 16 June 2008 - 11:56 PM Rate Topic: -----

#1 Mech_1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-June 08

Input/Output Stream Issue

Posted 16 June 2008 - 08:03 PM

#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


Is This A Good Question/Topic? 0
  • +

Replies To: Input/Output Stream Issue

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Input/Output Stream Issue

Posted 16 June 2008 - 08:20 PM

I personally would close the stream at the end of each "iteration" to ensure I don't have multiple streams trying to act on one file at the same time. Reopen the stream at the beginning of the next round.
Was This Post Helpful? 0
  • +
  • -

#3 Mech_1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-June 08

Re: Input/Output Stream Issue

Posted 16 June 2008 - 08:40 PM

Yeah, I noticed my .close was outside the while loop. I moved it to be at the end of the while loop however had the same problem.

I still do not understand I/O streams on why they can only access the file once, or how to set them the be beginning of file (which is what I thought iostream1.beg; was for).

But I have found the problem and the program does work now. There is a question in my code below asking about why the program doesnt work if the I/O streams are declared at that position.

(Code works how I originally wanted it to, however why cant the variables be initialized outside the while statement?)
#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


[b]-->QUESTION<--
	   //Why doesnt the program work if
	   //the ifstream and ofstream variables are
	   //declared here?[/b]


	do
	{
		ifstream inputstream1;  //<-- Moved this inside the Do While Loop
		ofstream outputstream1; //<-- Moved this inside the Do While Loop
		inputstream1.open("file.txt");
		if (inputstream1.fail() == true)
		{
			cout << "\nFailed to open file.txt" << endl;
			inputstream1.open("file.txt", ios::app);
		}
		while(inputstream1 >> Input)
		{
			cout << "The previous number(s) entered was: " << Input << endl;
		}
		inputstream1.close();


		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();
		cout << endl << "Performed While Loop" << endl;
		Number = 1;
		//inputstream1.beg;


	}while(GetMore());


	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');
	cin.ignore();
	return (Response == 'Y')? true:false;
}


If it is possible to initialize the stream variables before the Do While Loop, what would I have to do in order to get them to work?

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Input/Output Stream Issue

Posted 16 June 2008 - 08:55 PM

Well, variables inside a loop are local to only an execution of that loop, which works perfectly for what you need it to do (i.e. be done on each iteration and start afresh).

To access certain parts of the file you can "seek" through it using a pointer to set and manipulation positions and the like.


Your original code worked for me. It opened the file.txt file and wrote new data to it.
Was This Post Helpful? 0
  • +
  • -

#5 Mech_1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-June 08

Re: Input/Output Stream Issue

Posted 16 June 2008 - 11:37 PM

Yeah it did, however it didnt continuously do it if you put 'Y' as in Yes I woud like to do more I/O it wouldnt work after the first try.

I assume an I/O stream only works once per file until it is destroyed and remade?

I will have to do some research into 'seeking'

Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Input/Output Stream Issue

Posted 16 June 2008 - 11:56 PM

As far as I know, yes.

As for seeking, This is good

You'll want to declare a "get" pointer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1