2014-11-02 90 views
0
#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"); 
} 

我想用std :: string替换Cstring,但f.getline不会将其作为参数。字符串而不是Cstring C++

回答

3

成员函数getline()仅适用于原始字符数组。现代C++提供的免费功能std::getline()您可以使用std::string

#include <string> 

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

} 
+0

完美,谢谢 – Etixpp 2014-11-02 21:39:18

相关问题