3
votes

I want to read in a text file that has the name: abc.txt

The text file contains just a simple a, b, and c, each on their own line.

When I compile this using a Microsoft compiler, it compiles with no problem at all and I get output that I expect (see below):

a
b
c
(blank line here)

Here's the Borland compile line that I'm using:

bcc32 -w -ebor.exe main.cpp

Here's my main.cpp file that I'm using:

main.cpp

#include <iostream>
#include <fstream>
#include <string>
void printout(const std::string &file);

int main(void)
{
  printout("abc.txt");
  return 0;
}

void printout(const std::string &file)
{
  std::ifstream words;
  std::string   str;

  words.open(file);

  while(!words.eof())
  {
    std::getline(words, str);
    std::cout << str << "\n";
  }

  words.close();
}

The exact error that I'm getting in Borland is as follows:

Error E2285 main.cpp 17: Could not find a match for 'ifstream::open(const std::string)' in function printout(const std::string &)

I'm also getting a warning, but it seems like the only reason it's happening is due to the error preventing 'file' from being used:

Warning W8057 main.cpp 26: Parameter 'file' is never used in function printout(const std::string &)

Any help would be most appreciated, thank you.

1
Note: while (!words.eof()) is wrong, you may get a garbage output for the last line of the file. Change to while (std::getline(words, str))M.M

1 Answers

4
votes

Before C++11, std::ifstream::open required a const char *. Use this.

words.open( file.c_str() );