C++ 写入.dat文件会导致随机写入汉字

C++ 写入.dat文件会导致随机写入汉字,c++,c++11,visual-c++,C++,C++11,Visual C++,我有这个计划: using namespace std; class Motherboards { char Board_Manufacturer_Name[50]; char Socket_Type[50]; char Chipset_Type[50]; char Board_Name[50]; char Feature1[500]; char Feature2[500]; char Feature3[500]; float P

我有这个计划:

using namespace std;

class Motherboards
{
    char Board_Manufacturer_Name[50];
    char Socket_Type[50];
    char Chipset_Type[50];
    char Board_Name[50];
    char Feature1[500];
    char Feature2[500];
    char Feature3[500];
    float Price;

    public:
    void getdata();
    void displaydata();
};
void Motherboards::getdata()
{
    cout << " Enter Manufacturer Name :\n";
    cin.getline(Board_Manufacturer_Name, 50);
    cout << "\n Enter the Socket type :\n";
    cin.getline(Socket_Type, 50);
    cout << "\n Enter the Chipset Type :\n";
    cin.getline(Chipset_Type, 50);
    cout << "\n Enter the Board Name :\n";
    cin.getline(Board_Name, 50);
    cout << "\n Enter 3 main features for this Board :\n";
    cout << "\n1.) ";
    cin.getline(Feature1, 500);
    cout << "\n2.) ";
    cin.getline(Feature2, 500);
    cout << "\n3.) ";
    cin.getline(Feature3, 500);
    cout << "Price :\n";
    cin >> Price;
    cout << "\n";
}
void Motherboards::displaydata()
{
    cout << " Manufacturer : " << Board_Manufacturer_Name << endl;
    cout << " Socket : " << Socket_Type << endl;
    cout << " Chipset : " << Chipset_Type << endl;
    cout << " Board Name : " << Board_Name << endl;
    cout << " Features :\n" << "1.) " << Feature1 << endl
        << "2.) " << Feature2 << endl
        << "3.) " << Feature3 << endl;
    cout << " Price :\n" << Price;
    cout << "\n";
} 

int main()
{
    int x;
    system("cls");
    Motherboards M;
    fstream Infile;
    Infile.open("MotherboardList.dat", ios::in | ios::out | ios::app);
    if (!Infile)
    {
        cerr << "Error :";
    }
    cout << "Enter the details of the motherboard that you want to add :\n";
    M.getdata();
    Infile.write((char*)&M, sizeof(M));
    cout << "Do you wanna confirm ?";
    M.displaydata();
    cin >> x;
    cout << "Written :\n";
    Infile.read((char*)&M, sizeof(M));
    M.displaydata();
    Infile.close();

    return 0;
}
使用名称空间std;
类主板
{
纸板制造商名称[50];
字符插座_型[50];
字符芯片组_类型[50];
char Board_Name[50];
煤焦特性1[500];
煤焦特性2[500];
煤焦特性3[500];
浮动价格;
公众:
void getdata();
void displaydata();
};
void主板::getdata()
{

cout您创建的文件不是有效的文本文件,而是二进制文件。 您正在将C++结构体的内部结构以二进制表示形式写入文件。

如果希望文件可读,请在写入文件之前将结构序列化为字符串,使用JSON或XML


如果您想让c程序理解,请在读回后反序列化。

您是否尝试过使用调试器逐步检查代码?是的,尝试过使用调试器和不使用调试器。但是,您是否尝试逐步检查代码,通过观察变量的值?并查找代码何时执行与您的经验不同的操作CTActions?是的,它们都是单独工作的。它们是什么?逐步检查并调查代码,这是错误的行为。这就是调试——在开发过程中,你需要做很多工作,所以你学习得越早——从长远来看,你的情况越好。