Problem with getline

I am in windows xp, gcc, codeblocks.

When run the cout<<textFile<<endl; gives c and the file not opening.
Where i have the error?
Thank's

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<string> lines;

string inFile="c:\sfml2\license.txt";
getParLines(*inFile.c_str(),lines);

void getParLines(const char &textFile,vector<string>&lines)
{
  cout<<textFile<<endl;
  ifstream in(&textFile,ios::binary);
  string line;
  while (getline(in, line))
    {
      if (line.size()>0)
        {
          lines.push_back(line);
        }
    }
}


closed account (3hM2Nwbp)
You're passing a character to getParLines - I believe that this would give you the output that you expect.

1
2
3
4
5
6
7
8
9
10
11
12
13
/// We pass a pointer instead of a single character
void getParLines(const char* textFile, vector<string>& lines)
{
    cout << textFile << endl;
    ifstream in(textFile, ios::binary);
}

int main()
{
    vector<string> lines;
    string str = "SomeFile.txt";
    getParLines(str.c_str(), lines); //<-- Edit - .c_str()
}
Last edited on
You are passing textFile in as a character reference; the << operator wil get a single character, so it should just print "c". This is a really strange, non-standard way to attempt to pass a string to function though. Either pass it as a const string& or a const char*. It actually seems to work the way you have it on my system, so check your file path.

IMO, there is no reason for inFile to be a string, just declare and pass it to getParLines as a const char*. If you really want it as a string, pass it to getParLines as a const string&, and then use c_str() in the ifstream constructor. Also, if you do manage to open the file, you are opening it in binary mode when it is obviously a text file.
Ok i just open, many thank's.
Topic archived. No new replies allowed.