Etixpp
Etixpp

Reputation: 328

String instead of Cstring c++

#include <fstream>
#include <iostream>
using namespace std;

int main ( )
{
    fstream f;
    char cstring[256];
    f.open ( "test.txt", ios::in );
    short counter = 0;
    while ( !f.eof ( ) )
    {
        f.getline ( cstring, sizeof ( cstring ) );
        counter++;
        cout << cstring << endl;
    }
    cout << "Anzahl der Zeilen:" <<counter << endl;
    f.close ( );
    system ( "PAUSE" );
}

i would like to replace Cstring with a std::string but f.getline does not take it as parameter.

Upvotes: 0

Views: 206

Answers (1)

David G
David G

Reputation: 96845

The member function getline() only works for raw character arrays. Modern C++ provides the free function std::getline() which you can use for std::string:

#include <string>

std::string str;
while (std::getline(f, str)) {

}

Upvotes: 3

Related Questions