首页 > 编程语言 > 详细

C++读写文件的简单例子

时间:2016-06-19 12:54:38      阅读:234      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <fstream>
using namespace std;

void main()
{
    ofstream in;
    in.open("com.txt",ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建
    int i;
    char a=a;
    for(i=1;i<=26;i++)//将26个数字及英文字母写入文件
    {
        if(i<10)
        {
            in<<"0"<<i<<"\t"<<a<<"\n";
            a++;
        }
        else
        {
            in<<i<<"\t"<<a<<"\n";
            a++;
        }
    }
    in.close();//关闭文件
}


#include <iostream>
#include <fstream>
using namespace std;

void main()
{
    char buffer[256];
    fstream out;
    out.open("com.txt", ios::in);
    cout << "com.txt" << " 的内容如下:" << endl;
    while (!out.eof())
    {
        out.getline(buffer, 256, \n);//getline(char *,int,char) 表示该行字符达到256个或遇到换行就结束
        cout << buffer << endl;
    }
    out.close();
    cin.get();//cin.get() 是用来读取回车键的,如果没这一行,输出的结果一闪就消失了
}

 

C++读写文件的简单例子

原文:http://www.cnblogs.com/rain-1/p/5597843.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!