1 //用getline成员函数读取文件的内容 2 #include<fstream.h> 3 int main(){ 4 ifstream ifs("Data.dat"); 5 char buffer[80]; 6 int n=0; 7 8 while(!ifs.eof()){ 9 ifs.getline(buffer,sizeof(buffer)); 10 cout<<buffer<<endl; 11 n++; 12 } 13 cout<<"字符行数为:"<<n<<endl; 14 ifs.close(); 15 return 0; 16 }
Data.dat的文件内容为
1 sadf 2 asdfff 3 cxx 4 x