源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C++中简单读写文本文件的实现方法

  • 时间:2021-06-05 06:38 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C++中简单读写文本文件的实现方法
代码如下所示:
[u]复制代码[/u] 代码如下:
#include "stdafx.h" #include <iostream> #include <fstream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) {  //写入文件  ofstream ofs;  //提供写文件的功能  ofs.open("d:\\com.txt",ios::trunc); //trunc打开文件时,清空已存在的文件流,若不存在此文件则先创建  int i;  char a = 'a';  for(i = 1; i != 27; ++i)  {   if(i < 10)   {    ofs << "0" << i << "\t" << a << "\n";    a ++;   }   else   {    ofs << i << "\t" << a << "\n";    a ++;   }  }  ofs.close();  //读出文件到控制台  char buffer[256];  ifstream ifs; //提供读文件功能  ifs.open("d:\\com.txt",ios::in);//in--打开文件做读操作  cout << "d:\\com.txt" << "中的内容如下:" << endl;  while(!ifs.eof())  //判断是否达到stream的结尾  {   ifs.getline(buffer, 256, '\n'); //字符达到256个或遇到换行就结束   cout << buffer << endl;  }  ifs.close(); }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部