Link to home
Start Free TrialLog in
Avatar of businessesatoz
businessesatoz

asked on

c++ ifstream file input string help

I'm writing a program that will ask the user for the amount of time that they would like to see the file called "data.txt" displayed to the screen.. after getting the number of times to display data.txt from the user, i want to open it for reading and display the amount of time.. i'm stuck i get the amount of time and it only shows up one time.. also i want to put all this in a function called "DispFile" i have wrote what i have done so far below not sure what my errors is. the file data.txt contains the following "Here's Sulky Sue,

What shall we do?

Turn her face to the wall

Till she comes to."

so for example if user wants to see it 3 times it should display

*** Display #1:

Here's Sulky Sue,

What shall we do?

Turn her face to the wall

Till she comes to.




*** Display #2:

Here's Sulky Sue,

What shall we do?

Turn her face to the wall

Till she comes to.




*** Display #3:

Here's Sulky Sue,

What shall we do?

Turn her face to the wall

Till she comes to."

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;
string DispFile (ifstream &read);

int main(void)
{
	int how_many;
	cout <<"how many times would you like to see data.txt? ";
	cin >> how_many;

	ifstream in_stream;
	string mycatchstring;
	in_stream.open("data.txt");
	if(in_stream.fail())
	{
		cout <<"input file opening failed.";
		exit(1);
	}

DispFile(in_stream);


}


string DispFile (ifstream &read)
{
 string strings;
while(!read.eof())
	{
		getline (read,strings );
		cout<<strings <<endl;
	}
return strings;
}

Open in new window

Avatar of sarabande
sarabande
Flag of Luxembourg image

you best read the file once into an array. then you can display the contents in a loop as many time the user wants.

change interface of string DispFile(ifstream & read) into

void ReadFile(ifstream & read, std::vector<std::string> &  contents);

Open in new window


then change cout<<strings statement to contents.push_back(strings); what would add the current line to the vector.

you can output the vector in the main function for example by a for loop

for (size_t i = 0; i < contents.size(); ++i)

Open in new window


where you output contents[ i] in the loop block.

Sara
ASKER CERTIFIED SOLUTION
Avatar of Rahul Gade
Rahul Gade
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rahul Gade, the question likely is caused by an academical assignment. if so, ee experts should not post full code samples. generally, ee experts would not do the whole work for a questioner but would help only.

also your code has not a good design as it unnecessarily reads the same file multiple times.

Sara
businessesatoz, alternatively to using a vector you also could collect all lines of the text file into the return string.

then you would print out the returned string in main function in a loop.

to do so, you need two strings in DispFile. the one which is to contain all text with the linefeed characters. and a string which would take the current textline when you call getline(...).

to concatenate strings you simply would use the += operator like

strings += textline + "\n";

Open in new window


Sara
Avatar of businessesatoz
businessesatoz

ASKER

My goal is to do it this way, One way is to close the file, then reopen it. If you do that, then the file stream's “read” pointer will be reset to point to the very beginning of the stream. Thus, you'll be able to pass the stream object to the DispFile function as many times as you wish... although don't know how to do that.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the reason being is the book that i'm using is telling me to do it this way to make me get a better understanding.. can you provide an example sara? thanks i do understand your point of view.
I cannot use anything else besides what i know, i have not learned vectors yet.. so that's why i wanted to follow ", One way is to close the file, then reopen it. If you do that, then the file stream's “read” pointer will be reset to point to the very beginning of the stream. Thus, you'll be able to pass the stream object to the DispFile function as many times as you wish... although don't know how to do that." this way
thanks guys /gals :)
as told it is much better to have the

std::ifstream instream(filename.c_str());

Open in new window


in the DispFile and pass the filename as const std::string &  from main.

that way you can spare the open and always have a new filestream when calling DispFile.

of course the close must then moved to DispFile as well.

Sara