手机
当前位置:查字典教程网 >编程开发 >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++中简单读写文本文件的实现方法】相关文章:

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

C++获取zip文件列表方法

用C# 控制Windows系统音量的实现方法

求素数,用vector存储的实现方法

解析使用C++编写无错代码的方法技巧

解析VC中预编译头文件的深入分析

用c 获取文件MD5值的实现方法

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

c语言swap(a,b)值交换的4种实现方法

C++中事件机制的简洁实现及需要放弃的特性

精品推荐
分类导航