C++语言基础 例程 文本文件的读写

贺老师的教学链接  本课讲解


示例:将数据写入ASCII文件

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main( )
{
    int a[10];
    ofstream outfile("f1.dat",ios::out);//定义文件流对象,打开磁盘文件"f1.dat"
    if(!outfile)                        //如果打开失败,outfile返回0值
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    cout<<"enter 10 integer numbers:"<<endl;
    for(int i=0; i<10; i++) //向磁盘文件"f1.dat"输出数据
    {
        cin>>a[i];
        outfile<<a[i]<<" ";
    }
    cout<<"The numbers have been writen to file. "<<endl;
    outfile.close();       //关闭磁盘文件"f1.dat"
    return 0;
}


示例:从ASCII文件读入数据
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main( )
{
    int a[10],max,i,order;
    ifstream infile("f1.dat",ios::in);
    //定义输入文件流对象,以输入方式打开磁盘文件f1.dat
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    for(i=0; i<10; i++)
    {
        infile>>a[i];  //从磁盘文件读入10个整数,顺序存放在a数组中
        cout<<a[i]<<" ";
    }          //在显示器上顺序显示10个数
    cout<<endl;
    max=a[0];
    order=0;
    for(i=1; i<10; i++)
        if(a[i]>max)
        {
            max=a[i];                //将当前最大值放在max中
            order=i;                 //将当前最大值的元素序号放在order中
        }
    cout<<"max="<<max<<endl<<"order="<<order<<endl;
    infile.close();
    return 0;
}


示例:读写ASCII文件
#include<iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void save_to_file( );
void get_from_file();
int main( )
{
    save_to_file( );
    get_from_file( );
    return 0;
}
void save_to_file( )
{
    ofstream outfile("f2.dat");
    if(!outfile)
    {
        cerr<<"open f2.dat error!"<<endl;
        exit(1);
    }
    char c[80];
    cin.getline(c,80);
    for(int i=0; c[i]!=0; i++) 
        if(c[i]>='a' && c[i]<='z')
            outfile.put(c[i]);    
    outfile.close(); 
}
void get_from_file()
{
    char ch;
    ifstream infile("f2.dat",ios::in);
    if(!infile) 
   {
        cerr<<"open f2.dat error!"<<endl;
        exit(1);
    }
    ofstream outfile("f3.dat");
    if(!outfile)
    {
        cerr<<"open f3.dat error!"<<endl;
        exit(1);
    }
    while(infile.get(ch))
        outfile.put(ch-32);     
    infile.close( );     
    outfile.close();  
}


示例:在显示器上输出文件
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void display_file(char *filename);

int main( )
{
    display_file("f3.dat");
    return 0;
}
void display_file(char *filename)
{
    ifstream infile(filename,ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    char ch;
    while(infile.get(ch))
        cout.put(ch);
    cout<<endl;
    infile.close();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迂者-贺利坚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值