Busch
Busch

Reputation: 959

C ++ ifstream I/O?

I'm experimenting with C++ file I/O, specifically fstream. I wrote the following bit of code and as of right now it is telling me that there is no getline member function. I have been told (and insisted still) that there is a member function getline. Anybody know how to use the getline member function for fstream? Or perhaps another way of getting one line at a time from a file? I'm taking in two file arguments on the command line with unique file extensions.

./fileIO foo.code foo.encode

#include <fstream>
#include <iostream>  
#include <queue>
#include <iomanip>
#include <map>
#include <string>
#include <cassert>
using namespace std;
int main( int argc, char *argv[] )
{
  // convert the C-style command line parameter to a C++-style string,
  // so that we can do concatenation on it
  assert( argc == 2 );
  const string foo = argv[1];

  string line;string codeFileName = foo + ".code";

  ifstream codeFile( codeFileName.c_str(), ios::in );
  if( codeFile.is_open())
  {
  getline(codeFileName, line);
  cout << line << endl;
  }
  else cout << "Unable to open file" << endl;
  return 0;
}

Upvotes: 0

Views: 236

Answers (2)

john
john

Reputation: 87959

Typo

getline(codeFileName, line);

should be

getline(codeFile, line);

I guess the lesson is you have to learn how to interpret compiler error messages. We all make certain kinds of mistakes and learn the compiler errors they tend to generate.

Upvotes: 1

Collin
Collin

Reputation: 12287

getline(codeFileName, line);

Should be

getline(codeFile, line);

You're passing in the file name, not the stream.

By the way, the getline you're using is a free function, not a member function. In fact, one should avoid the member function getline. It's much harder to use, and harkens back to a day when there was no string in the standard library.

Upvotes: 1

Related Questions