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

运用C++实现文件的写操作

2012-12-19 
使用C++实现文件的写操作// File.cpp : 定义控制台应用程序的入口点。//#include stdafx.h#include iost

使用C++实现文件的写操作

// File.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

/**
*功能:将26个数字和字母写入到磁盘文件中
*@author 超仔
**/

void main()
{
ofstream in;//主要用来写文件
in.open("D:\\disk\\file.txt",ios::trunc);//ios::trunc表示在打开文件前将文件清空,当文件不存在时则创建

int i;
char str = 'a';

for(i=1;i<27;i++)
{
if(i<10)
{
in<<"0"<<i<<"\t"<<str<<"\n";//其中\t表示一个Tab键,\n表示一个换行符
str++;
}
else
{
in<<i<<"\t"<<str<<"\n";
str++;
}
}

in.close();//关闭文件
}

热点排行