C++文件输入与读取

c++二进制文件无法读取正常
include
#include
#include
#include
using namespace std;

class Student{
public:
Student(){
}
Student(int id,char n[],double cpp,double math,double english){
this->id=id;
strcpy(name,n);
this->cpp=cpp;
this->math=math;
this->english=english;
sum=cpp+math+english;
}
double set(int id,char n[],double cpp,double math,double english){
this->id=id;
strcpy(name,n);
this->cpp=cpp;
this->math=math;
this->english=english;
sum=cpp+math+english;
}
int get_id(){
return id;
}

    double get_cpp(){
        return cpp;
    }
    double get_math(){
        return math;
    }
    double get_english(){
        return english;
    }
    double get_sum(){
        return sum;
    }

    int id;
    char name[100];
    double cpp;
    double math;
    double english;
    double sum;

};
int main(){
Student num[101];
int Id;
double English,Math,Cpp;
char Name[100];
char n[100];
fstream f1;
f1.open("score.dat",ios::in);
if(f1.fail()){
cout<<"打开文件失败!"<<endl;
exit(1);
}
for(int i=0;i<100;i++){
f1>>Id>>Name>>Cpp>>Math>>English;
num[i].set(Id,Name,Cpp,Math,English);
}
f1.close();
fstream f2;
f2.open("binary_score.dat",ios::out|ios::binary);
if(f2.fail()){
cout<<"打开文件失败!"<<endl;
exit(2);
}
for(int i=0;i<100;i++){
f2.write((char*)&num[i],sizeof(num[i]));
}
cout<<"请输入信息:"<<endl;
cin>>Id>>Name>>Cpp>>Math>>English;
Student s(Id,Name,Cpp,Math,English);
f2.write((char*)&s,sizeof(s));
f2.close();
Student x;
fstream f3;
f3.open("binary_score.dat",ios::in|ios::binary);
if(f3.fail()){
cout<<"打开文件失败!"<<endl;
exit(3);
}
while(!f3.eof()){
f3.read((char*)&x,sizeof(x));
cout<<x.get_id()<<" "<<x.name<<" "<<x.get_cpp()<<" "<<x.get_math()<<" "<<x.get_english()<<" "<<x.get_sum()<<endl;
}
f3.close();
return 0;
}

img

(1)score.dat文件用记事本打开,选择“文件” -- "另存为",再另存为对话框的编码中,选择ANSI,点击保存。

img


(2)你的代码有点问题,读文件的时候需要记录读取的条数,写文件的时候不要写死100,代码修改了一下,运行结果如下:

img

代码修改如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Student {
public:
    Student() {
    }
    Student(int id, char n[], double cpp, double math, double english) {
        this->id = id;
        strcpy(name, n);
        this->cpp = cpp;
        this->math = math;
        this->english = english;
        sum = cpp + math + english;
    }
    double set(int id, char n[], double cpp, double math, double english) {
        this->id = id;
        strcpy(name, n);
        this->cpp = cpp;
        this->math = math;
        this->english = english;
        sum = cpp + math + english;
        return 0;
    }
    int get_id() {
        return id;
    }

    double get_cpp() {
        return cpp;
    }
    double get_math() {
        return math;
    }
    double get_english() {
        return english;
    }
    double get_sum() {
        return sum;
    }

    int id;
    char name[100];
    double cpp;
    double math;
    double english;
    double sum;
};
int main() {
    Student num[101];
    int Id;
    double English, Math, Cpp;
    char Name[100];
    char n[100];
    fstream f1;
    f1.open("score.dat", ios::in);
    if (f1.fail()) {
        cout << "打开文件失败!" << endl;
        exit(1);
    }
    int size = 0;
    for ( size = 0; !f1.eof(); size++) {
        f1 >> Id >> Name >> Cpp >> Math >> English;
        num[size].set(Id, Name, Cpp, Math, English);
    }
    f1.close();
    fstream f2;
    f2.open("binary_score.dat", ios::out | ios::binary);
    if (f2.fail()) {
        cout << "打开文件失败!" << endl;
        exit(2);
    }
    for (int i = 0; i < size; i++) {
        f2.write((char*)&num[i], sizeof(num[i]));
    }
    cout << "请输入信息:" << endl;
    cin >> Id >> Name >> Cpp >> Math >> English;
    Student s(Id, Name, Cpp, Math, English);
    f2.write((char*)&s, sizeof(s));
    f2.close();
    Student x;
    fstream f3;
    f3.open("binary_score.dat", ios::in | ios::binary);
    if (f3.fail()) {
        cout << "打开文件失败!" << endl;
        exit(3);
    }
    while (!f3.eof()) {
        if( f3.read((char*)&x, sizeof(x)) )
            cout << x.get_id() << " " << x.name << " " << x.get_cpp() << " " << x.get_math() << " " << x.get_english() << " " << x.get_sum() << endl;
    }
    f3.close();
    return 0;
}

我测试正常啊

img

img

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

class Student
{
public:
    Student()
    {
    }
    Student(int id, char n[], double cpp, double math, double english)
    {
        this->id = id;
        strcpy(name, n);
        this->cpp = cpp;
        this->math = math;
        this->english = english;
        sum = cpp + math + english;
    }
    void set(int id, char n[], double cpp, double math, double english)
    {
        this->id = id;
        strcpy(name, n);
        this->cpp = cpp;
        this->math = math;
        this->english = english;
        sum = cpp + math + english;
    }
    int get_id()
    {
        return id;
    }

    double get_cpp()
    {
        return cpp;
    }
    double get_math()
    {
        return math;
    }
    double get_english()
    {
        return english;
    }
    double get_sum()
    {
        return sum;
    }

    int id;
    char name[100];
    double cpp;
    double math;
    double english;
    double sum;
};
int main()
{
    Student num[101];
    int Id;
    double English, Math, Cpp;
    char Name[100];
    char n[100];
    fstream f1;
    f1.open("score.dat", ios::in);
    if (f1.fail())
    {
        cout << "打开文件失败!" << endl;
        exit(1);
    }
    for (int i = 0; i < 3; i++)
    {
        f1 >> Id >> Name >> Cpp >> Math >> English;
        num[i].set(Id, Name, Cpp, Math, English);
    }
    f1.close();
    fstream f2;
    f2.open("binary_score.dat", ios::out | ios::binary);
    if (f2.fail())
    {
        cout << "打开文件失败!" << endl;
        exit(2);
    }
    for (int i = 0; i < 3; i++)
    {
        f2.write((char *)&num[i], sizeof(num[i]));
    }
    cout << "请输入信息:" << endl;
    cin >> Id >> Name >> Cpp >> Math >> English;
    Student s(Id, Name, Cpp, Math, English);
    f2.write((char *)&s, sizeof(s));
    f2.close();
    Student x;
    fstream f3;
    f3.open("binary_score.dat", ios::in | ios::binary);
    if (f3.fail())
    {
        cout << "打开文件失败!" << endl;
        exit(3);
    }
    while (!f3.eof())
    {
        if(!f3.read((char *)&x, sizeof(x)))
            break;
        cout << x.get_id() << " " << x.name << " " << x.get_cpp() << " " << x.get_math() << " " << x.get_english() << " " << x.get_sum() << endl;
    }
    f3.close();
    return 0;
}

你是不是score.dat文件保存的编码不对. score.dat文件用gbk编码或者utf-8编码重新保存下.
具体哪种编码要与你的运行环境而定,gbk编码或者utf-8编码都试一下

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632