• Advertisement

Archived

This topic is now archived and is closed to further replies.

Noob Q's on getline()

This topic is 5066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys im just ur typical noob screwing around with C++ and i really gotta get this straight! Heres the code:
#include <fstream>
#include <iostream>
#include <string>

void GetLines();
void DisplayLines();
void OpenFile();
void RunIt();

using namespace std;

string str;
ifstream ifs;

void main() {

	RunIt();

}

void GetLines() {
	for(int i = 0; i < 4; i++)
		getline(ifs, str);
}

void DisplayLines() {
	cout << str;
}

void OpenFile() {
	ifs.open("C:/Program Files/Testing.txt");
}

void RunIt() {
	OpenFile();
	GetLines();
	DisplayLines();
}
I have a file named Testing.txt in Program Files (obviously) but it doesnt return their strings to the console like I asked it to Did I ask wrong or is my compiler possesed? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I think you should test to make sure your file is opening.

You're getting all the lines but you're only displaying once, the last line is probably empty therefore it doesn't display anything. The simple way to fix this is to call DisplayLines() in your GetLines() "for loop".

[edited by - tiffany_smith on April 13, 2004 10:37:03 PM]

Share this post


Link to post
Share on other sites
getline() replaces whatever str is, it does not add it to the end of str. If you wanted it to add you could try having a temp string, getline into that, then add that to the end of str with str += tmp, or something.

Share this post


Link to post
Share on other sites
There''s a way simpler way to output what''s inside a file...

I won''t show you unless you ask, I don''t want to spoil the fun

Share this post


Link to post
Share on other sites
RunIt() is thoroughly redundant. Get rid of it.

You''re overusing functions. OpenFile() is a bad function because it''s effects are literally distributed: it affects an object with global scope, providing no guarantees that the file can not be closed in ways other than the calling of some (non-existent) complement to OpenFile().

DisplayLines() will display only one line.

When GetLines() is done, only the last line will be stored in str. There is also a better way to get all the lines in the file (your current version only gets 4 lines; what if the file has three lines? or three thousand?):

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

using namespace std;

int PrintLines(const char * filename)
{
ifstream fin(filename);
if( !fin )
{
cerr << "Failed to open file " << filename << endl;
return -1;
}

while( !fin.eof() )
{
getline( fin, str );
cout << str << endl;
}
return 0;
}

There''s a better way to simply print out all the lines in a file, but it''s not instructive for our purposes (it involes a std::ostream_iterator and std::copy).

If you want to retrieve the data in a file by line for later use, do something like this:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
vector<string> lines;
ifstream fin;
string line;

cout << "Filename: ";
cin >> line; // reusing the variable because i''m lazy

fin.open( line.c_str() );
if( fin.fail() )
{
cerr << "Unable to open file " << line << ". Exiting..." << endl;
return -1;
}

while( !fin.eof() )
{
getline( fin, line );
lines.push_back( line );
}

// lines now contains all the lines of data in the file


...

return 0;
}

Share this post


Link to post
Share on other sites

  • Advertisement