手机
当前位置:查字典教程网 >编程开发 >C语言 >C++中简单读写文本文件的实现方法
C++中简单读写文本文件的实现方法
摘要:代码如下所示:复制代码代码如下:#include"stdafx.h"#include#includeusingnamespacestd;in...

代码如下所示:

复制代码 代码如下:

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

}

【C++中简单读写文本文件的实现方法】相关文章:

linux c 获取本机公网IP的实现方法

Java3D实例之创建空间几何模型的实现方法

判断机器大小端的两种实现方法

C++中const的实现机制深入分析

c++大数阶乘的实现方法

基于一个简单定长内存池的实现方法详解

C++可变参数的实现方法

数组中求第K大数的实现方法

C++中求组合数的各种方法总结详解

给ActiveX签名的实现方法详解

精品推荐
分类导航