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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-18 我要评论
想了解C++中简单读写文本文件的实现方法的相关内容吗,在本文为您仔细讲解C++ 读写文本文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++,读写文本文件,下面大家一起来学习吧。
代码如下所示:
复制代码 代码如下:

#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();
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们