首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

如何读取txt文件中的内容

2012-08-03 
怎么读取txt文件中的内容?包括读取其中的内容和写入,请贴完整代码及原理注释[解决办法]C/C++ code#include

怎么读取txt文件中的内容?
包括读取其中的内容和写入,

请贴完整代码及原理注释

[解决办法]

C/C++ code
#include <iostream>#include <fstream>using namespace std;int main(int argc, char** argv){    char aline[1024];    // 想文本文件写入    ofstream fos("e:/file.txt");    if(!fos)    {        cout << "Can not open file..." << endl;    }    fos << "This is line 1 to be written into file.txt" << endl;    fos << "This is line 2 to be written into file.txt" << endl;    fos << "This is line 3 to be written into file.txt" << endl;    fos.close();    // 读文本文件内容    ifstream fis("e:/file.txt");    if(!fis)    {        cout << "Can not open file..." << endl;    }    while(fis)    {        memset(aline, 0, 100);        fis.getline(aline, 100);        if(aline[0] == 0) break;        cout << aline << endl;    }    return 0;} 

热点排行