C++文件操做之----逐行讀取文本中的數據

ifstream 有一個getline() 函數 逐行讀取ios

可是該函數不支持string類型ide

getline(char *p,int); 這樣 必須 char[] 來作緩衝區,函數

學習Console程序時 , 用到過學習

string line;spa

cin >>line;ci

還有一種方法是
std::getline(cin,line);get


那麼在 ifstream 中也可使用次方法string

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main(int argc,char **argv){
    ifstream out;
    string str = "d:\\text.txt";
    out.open(str.c_str(), ios::in);
    string line;
    while(!out.eof()){
        std::getline(out,line);
        cout <<line<<endl;
    }
    out.close();
    return 0;
}
相關文章
相關標籤/搜索