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."
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
Sara
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";
Sara
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
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
ASKER
thanks guys /gals :)
as told it is much better to have the
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
std::ifstream instream(filename.c_str());
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
change interface of string DispFile(ifstream & read) into
Open in new window
then change cout<<strings statement to contents.push_back(strings
you can output the vector in the main function for example by a for loop
Open in new window
where you output contents[ i] in the loop block.
Sara