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

简介: 贺老师的教学链接  本课讲解示例:将数据写入ASCII文件#include <iostream>#include <fstream>#include <cstdlib>using namespace std;int main( ){ int a[10]; ofstream outfile("f1.dat",ios::out);

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


示例:将数据写入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();
}


目录
相关文章
|
1月前
|
编译器 C++
C++语言预处理器学习应用案例
【4月更文挑战第8天】C++预处理器包括条件编译、宏定义和文件包含等功能。例如,条件编译用于根据平台选择不同代码实现,宏定义可简化常量和变量名,文件包含则用于整合多个源文件。示例中展示了如何使用`#ifdef`等指令进行条件编译,当`DEBUG`宏定义时,`PRINT_LOG`会打印调试信息,否则不执行。
14 1
|
14天前
|
Linux 程序员 图形学
C++语言在现代软件开发中的应用与实践
C++语言在现代软件开发中的应用与实践
20 2
|
14天前
|
存储 程序员 C语言
深入理解C++:从语言特性到实践应用
深入理解C++:从语言特性到实践应用
24 3
|
14天前
|
存储 算法 安全
C++语言深度探索:从基础到实践
C++语言深度探索:从基础到实践
14 2
|
26天前
|
机器学习/深度学习 人工智能 大数据
开发语言漫谈-C++
C++最初的名字为“带类的C”
|
26天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。
|
1月前
|
存储 C++ iOS开发
C++文件操作(文本文件的读写+二进制文件的读写)
C++文件操作(文本文件的读写+二进制文件的读写)
|
1月前
|
存储 C++
C++语言学习指针和引用应用案例
C++中的指针和引用用于高效操作内存。示例展示指针和引用的基本用法:指针`*p`存储变量`a`的地址,引用`&x`在函数调用中实现值交换而无需复制。此外,引用`update(&x)`可直接修改原变量,指针`p`在数组操作中用于遍历和访问不同部分。
12 2
|
1月前
|
C++
C++语言学习数组和字符串应用案例
【4月更文挑战第8天】该文展示了C++中数组和字符串的应用案例。数组示例定义了一个整数数组并访问、修改其元素,计算了元素之和。字符串示例中,定义了一个字符串并遍历、修改字符,进行了字符串拼接、查找子字符串及替换操作。
12 3
|
1月前
|
C++
C++语言学习文件操作应用案例
C++文件操作示例:创建`ofstream`对象写入&quot;Hello, World!&quot;到`output.txt`,刷新缓冲区,然后使用`ifstream`读取并打印文件内容。如果文件打开失败,程序将显示错误信息并返回1。
12 3