Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.txt文件显示中文符号/c++; 所以我在用C++编写这个程序,所有的东西看起来都很好,直到我打开.txt文件…我所能看到的只是中国符号。。有人有想法吗_C++ - Fatal编程技术网

.txt文件显示中文符号/c++; 所以我在用C++编写这个程序,所有的东西看起来都很好,直到我打开.txt文件…我所能看到的只是中国符号。。有人有想法吗

.txt文件显示中文符号/c++; 所以我在用C++编写这个程序,所有的东西看起来都很好,直到我打开.txt文件…我所能看到的只是中国符号。。有人有想法吗,c++,C++,这是我的密码。不要介意未完成的搜索功能和其他东西 #include <iostream> #include <fstream> using namespace std; fstream data_file; struct depositor { char name[20]; char last_name[30]; char address[60]; }; void add_depositor(depositor p_Info); void s

这是我的密码。不要介意未完成的搜索功能和其他东西

#include <iostream>
#include <fstream>

using namespace std;

fstream data_file;
struct depositor
{
    char name[20];
    char last_name[30];
    char address[60];
};

void add_depositor(depositor p_Info);
void search_depositor();

void add_depositor(depositor p_Info)
{
    data_file.open("Data.txt", ios::app);
    if (data_file.fail())
    {
        cout << "Error while opening the file!";
        exit(1);
    }
    else
    {
        data_file.write((char*)(&p_Info), sizeof(depositor));
        data_file.close();
    }
}

int menu()
{
    int choice;
    do
    {
        cout << "\n* Menu *";
        cout << "\n* 1. Add depositor! *";
        cout << "\n* 2. Search for depositor! *";
        cout << "\n* 3. Exit program! *";
        cout << "\n* Enter your choice: ";
        cin >> choice;
        cout << "\n* You chose: " << choice;
    } while (choice < 1 || choice > 4);
    return choice;
}
void main()
{
    depositor p; int choice;
    do
    {
        choice = menu();
        switch (choice)
        {
        case 1: cout << "\n Enter first name: ";
            cin >> p.name;
            cout << "\n Enter last name:  ";
            cin >> p.last_name;
            cin.clear();
            cin.ignore(2000, '\n');
            cout << "\n Enter address: ";
            cin.getline(p.address, 60);
            add_depositor(p); break;
        case 2: cout << "";
        default: cout << "\n* End of program! *";
        }
    } while (choice != 4);
}
#包括
#包括
使用名称空间std;
fstream数据文件;
结构储户
{
字符名[20];
char last_name[30];
字符地址[60];
};
无效添加存款人(存款人p_信息);
无效搜索_存款人();
无效添加存款人(存款人p_信息)
{
data_file.open(“data.txt”,ios::app);
if(data_file.fail())
{

cout将对象数据写入文件的方式:

  data_file.write((char*)(&p_Info), sizeof(depositor));
存储的数据未编码为可由文本编辑器读取。但是,它可用于对象序列化,以便以后检索

要以文本形式存储数据(可通过记事本查看),请执行以下操作:

 data_file << p_Info.name << " "<<p_Info.last_name<<" " << p_Info.address << "\n";

data\u file我认为您的问题在于,您只是将结构中的任何内容转储到文本文件中(无论它是否是好数据)。您可能希望使用空格分隔各个方法和输出,您希望看到什么?记事本误解了文件,因为其中包含二进制数据。您只需写入文本。@markransem:what“binary data”是吗?@MarkRansom我只是在写text@Puff不,你在写一个结构,里面有未初始化的字符。谢谢你的快速响应!:)一个向上的投票会比一个感谢你的评论更好!:)我刚刚做了我的acc…它说我至少需要15个代表:D你说的“二进制形式”是什么?没有“二进制格式”与“ASCII格式”“--这是对文件流和终端进行控制的抽象,在这里不起作用。在这两段代码中,您只是将字节写入文件,而这些字节表示字符。这些字符可以也将表示为ASCII。数据在运行时保存在内存中的方式。@ShikharBhardwaj:那么告诉我这是如何实现的。”这两个例子的“方式”不同。
data_file.open("Data.dat", ios::app | ios::binary); //Its not a text file anymore and has to be opened in binary mode.
if(!data_file){
  cout<<"\nError";
}
else{
  data_file.write((char*)(&p_Info), sizeof(depositor));
}
data_file.close();
data_file << p_Info.name << ' ' << p_Info.last_name << ' ' << p_Info.address;