Noob Q's on getline()

Started by
3 comments, last by RedRabbit 20 years ago
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!
//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.
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]
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.
My stuff.Shameless promotion: FreePop: The GPL god-sim.
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
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;}

This topic is closed to new replies.

Advertisement