C++读取文件操作

C++的标准库头文件为:#include<fstream>同时包含了fsteam、ifstream、ofstream下面先简述一下这三者的区别:

fstream;且同时具有 ofstream 和 ifstream 两种功能,它可以创建文件,向文件写入信息,从文件读取信息。

ifstream ;该数据类型表示输入文件流,用于从文件读取信息。

ofstream:数据类型表示输出文件流,用于创建文件并向文件写入信息。注意若文件存在,则先清空文件再写入。

ios::in

打开文件用于读取。

ios::out

打开文件用于写入。

ios::ate

文件打开后定位到文件末尾。

ios::app

追加模式。所有写入都追加到文件末尾。

ios::binary

二进制方式

ios::trunc

如果文件已存在则先删除该文件

下面的代码是先创建一个文件写入数据,然后将数据传入另一个文件。

#include<fstream>
#include<iostream>
#include<windows.h>
using namespace std;
//从键盘上读取字符的函数
void read_save() {
    char c[80] ;
    ofstream outfile("e://f.txt");//以输出方式打开文件
    if (!outfile) {
        cerr << "open error!" << endl;//注意是用的是cerr 
        exit(1);
    }
    cin.getline(c, 80);//从键盘读入一行字符
    for (int i = 0; c[i] != 0; i++) //对字符一个一个的处理,直到遇到'/0'为止 
        if (c[i] >= 65 && c[i] <= 90 || c[i] >= 97 && c[i] <= 122) {//保证输入的字符是字符 
            outfile.put(c[i]);//将字母字符存入磁盘文件 
            cout << c[i] << " ";
        }
    cout << endl;
    outfile.close();
}
void creat_data() {
    char ch;
    ifstream Read_File("e://f.txt");//打开文件 
    if (!Read_File) {
        cerr << "open error!" << endl;
        exit(1);
    }
    ofstream Write_File("e://f1.txt");//定义输出流f1.dat文件
    if (!Write_File) {
        cerr << "open error!" << endl;
        exit(1);
    }
    while (Read_File.get(ch)) {//当读取字符成功时 
        if (ch <= 122 && ch >= 97)
        Write_File.put(ch);
    }
    cout << endl;
    cout << "文件写入成功!" << endl;
    Read_File.close();
    Write_File.close();
}
int main() {
    system("color 0A");//控制颜色
    read_save();
    creat_data();
    system("pause");
    return 0;
}

写文件的时候出现乱码 

#include<fstream>
#include<iostream>
//#include<windows.h>
using namespace std;
//从键盘上读取字符的函数

struct Book {
    double price;
    char name[100];
    char Locate[30];
}book[100];
string path = "e://book.txt";

//读取文件函数 将txt文件中的数据导入结构体
void Read_File(int &sum)
{
    int n = 0;
    char first_line[200];
    char second_line[200];
    ifstream myfile(path, ios::in);
    if (!myfile.is_open())
    {
        cout << "Sorry You Have Failed to Open the File!" << endl;
        exit(1);
    }
    else
    {
        myfile.getline(first_line, 200); 
        myfile.getline(second_line, 200);
        while (!myfile.eof())
        {
            myfile >> book[n].Locate >> book[n].name >> book[n].price;
            n++;
        }
    }
    sum = n;
    cout << "一共有" << sum << "个图书的信息" << endl;
    myfile.close();
    cout << "You have read the file successfully!" << endl;
}

//写入文件函数 将更新后的数据写入指定文件
void Out_to_File(int sum)
{
    int n = 0;
    ofstream file;

    file.open("e://book1.txt", ios::out | ios::trunc);

    if (!file.is_open())
    {
        cout << "You have failed to open the file!" << endl;
        exit(1);
    }
    while (n < sum)
    {
        file << book[n].Locate << "\t" << book[n].name << "\t" << book[n].price << endl;
        n++;
    }
    cout << sum << endl;
    file.close();
}

//显示函数 将图书信息打印在显示屏上
void Display_Information(int sum) {
       //double x;
       //int n = 0;
       // char a, b;
       // ifstream file("e://book1.txt", ios::in);
       // while (file.is_open()&&n<sum)
       // {
       //     file >> x >> a >> b;
       //     cout << x << a << b << endl;
       //     n++;
       // }
       // file.close();
    int n = 0;
    while (n < sum)
    {
        cout << book[n].Locate << book[n].name << book[n].price << endl;
        n++;
    }
       
}

int main() {
    //system("color 0A");//控制颜色
    int sum = 0;
    Read_File(sum);
    Out_to_File(sum);
    Display_Information(sum);
    system("pause");
    return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值