c++ - 我的程序控制台输出中出现未知的 ascii 代码字符

标签 c++ visual-studio

我的程序主要是询问用户输入,并将登录数据、用户名等数据保存在文本文件中,并使用同一文件检索数据并显示在输出控制台中。程序执行时,用户可以在选项1到6之间进行选择,选项6用于退出应用程序,其余选项1到5用于用户输入数据和查看文件中存储的数据。

当程序在控制台显示数据时,我看到很多不必要的 ASCii 代码。为什么它会出现,我该如何让它们消失?谢谢!!

int main() {
    //Considering the max length of data entered (name) to be 15.
    char data[15];
    int n = 0, option = 0, count_n = 0;
    //This is the initial mark alloted to a subject.
    string empty = "00";
    string proctor = "";
    //Name of the file in which DB is stored.
    ifstream f("Example.txt");
    string line;

    //The following for loop counts the total number of lines in the file.
    for (int i = 0; std::getline(f, line); ++i) {
        count_n++;
    }

    while (option != 6) {
        //This prints out all the available options in the DB
        cout << "\nAvailable operations: \n1. Add New Students\n2."
            << "Student Login\n3. Faculty Login\n4. Proctor Login\n5. Admin View\n"
            << "6. Exit\nEnter option: ";
        cin >> option;

        if (option == 1) {
            cout << "Enter the number of students: ";
            cin >> n;

            count_n = count_n + n;

            for (int i = 0; i < n; i++) {
                ofstream outfile;
                outfile.open("Example.txt", ios::app);
                //The entire data of a single student is stored line-by-line.
                cout << "Enter your registration number: ";
                cin >> data;
                outfile << data << "\t";

                cout << "Enter your name: ";
                cin >> data;
                int len = strlen(data);

                while (len < 15) {
                    data[len] = ' ';
                    len = len + 1;
                }
                outfile << data << "\t";
                //Inserting empty data initially into the file
                outfile << empty << "\t";
                outfile << empty << "\t";
                cout << "\b \b";

                cout << "Enter your proctor ID: ";
                cin >> proctor;

                outfile << proctor << endl;

            }
        }

        else if (option == 2) {
            char regno[9];
            cout << "Enter your registration number: ";
            cin >> regno;
            ifstream infile;
            int check = 0;
            infile.open("Example.txt", ios::in);

            //This loop prints out the data according to the registration number specified.
            while (infile >> data) {
                if (strcmp(data, regno) == 0) {
                    cout << "\nRegistration Number: " << data << endl;
                    infile >> data;
                    cout << "Name: " << data << endl;

                    infile >> data;
                    cout << "CSE1001 mark: " << data << endl;

                    infile >> data;
                    cout << "CSE1002 mark: " << data << endl;

                    infile >> data;
                    cout << "Proctor ID: " << data << endl;

                    infile.close();
                    check = 1;
                }
            }

            if (check == 0) {
                cout << "No such registration number found!" << endl;
            }

        }

        //This loop is used to view and add marks to the database of a student.
        else if (option == 3) {
            char subcode[7];
            cout << "Enter your subject code: ";
            cin >> subcode;
            string code1 = "CSE1001", code2 = "CSE1002", mark = "";
            ifstream infile;
            int check = 0;

            cout << "\nAvailable operations: \n1. Add data about marks\n"
                << "2. View data\nEnter option: ";
            cin >> option;

            if (option == 1) {
                cout << "Warning! You would need to add mark"
                    << "details for all the students!" << endl;
                for (int i = 0; i < count_n; i++) {
                    fstream file("Example.txt");

                    //The seek in file has been done according to the length
                    //of the data being inserted. It needs to adjusted accordingly
                    //for diffferent lengths of data.

                    if (strcmp(subcode, code1.c_str()) == 0) {
                        file.seekp(26 + 37 * i, std::ios_base::beg);
                        cout << "Enter the mark of student#" << (i + 1) << " : ";
                        cin >> mark;
                        file.write(mark.c_str(), 2);
                    }

                    if (strcmp(subcode, code2.c_str()) == 0) {
                        file.seekp(29 + 37 * i, std::ios_base::beg);
                        cout << "Enter the mark of student#" << (i + 1) << " : ";
                        cin >> mark;
                        file.write(mark.c_str(), 2);

                    }
                }

            }

            //This loop is used to view marks of a student.
            //The extra infile commands have been used to get a specific mark 
            //only since the data has been seperated by a tabspace.

            else if (option == 2) {
                infile.open("Example.txt", ios::in);
                if (strcmp(subcode, code1.c_str()) == 0) {
                    cout << "Registration number - Marks\n" << endl;
                    while (infile >> data) {
                        cout << data;
                        infile >> data;
                        infile >> data;
                        cout << " - " << data << endl;
                        infile >> data;
                        infile >> data;
                        check = 1;
                    }
                }

                infile.close();
                infile.open("Example.txt", ios::in);

                if (strcmp(subcode, code2.c_str()) == 0) {
                    cout << "Registration number - Marks\n" << endl;
                    while (infile >> data) {
                        cout << data;
                        infile >> data;
                        infile >> data;
                        infile >> data;
                        cout << " - " << data << endl;
                        infile >> data;
                        check = 1;

                    }
                }
            }

            infile.close();

            if (check == 0) {
                cout << "No such subject code found!" << endl;
            }

        }

        //This loop displays all the details of students under the same proctor ID. 

        else if (option == 4) {
            char procid[7];
            cout << "Enter your proctor ID: ";
            cin >> procid;
            int check = 1;
            char temp1[100], temp2[100], temp3[100];
            char temp4[100], id[100];
            ifstream infile;
            infile.open("Example.txt", ios::in);

            while (infile >> temp1) {
                infile >> temp2;
                infile >> temp3;
                infile >> temp4;
                infile >> id;

                if (strcmp(id, procid) == 0) {
                    cout << "\nRegistration Number: " << temp1 << endl;
                    cout << "Name: " << temp2 << endl;
                    cout << "CSE1001 Mark: " << temp3 << endl;
                    cout << "CSE1002 Mark: " << temp4 << endl;
                    check = 1;
                }
            }

            if (check == 0) {
                cout << "No such proctor ID found!" << endl;
            }
        }

        //This loop acts as an admin view to see all the data in the file.

        else if (option == 5) {
            char password[25];
            cout << "Enter the admin password: ";
            cin >> password;

            //This variable value can be changed according to your requirement 
            //of the administrator password.

            string admin_pass = "admin";

            if (strcmp(password, admin_pass.c_str()) == 0) {
                cout << "Reg No.     \tName\tCSE1001\tCSE1002\tProctor ID" << endl;
                ifstream infile;
                infile.open("Example.txt", ios::in);
                char data[20];

                while (infile >> data) {
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << endl;

                }
            }
        }
    }
}

这是输出:

enter image description here

最佳答案

╠ 字符是一个 box drawing character .在代码页 437它由十六进制值 0xCC 表示.

在本例中它是一个 magic hex value这意味着它是未初始化的堆栈内存。

data array 有 20 个字符长,但它会退化为一个指针和 operator<<输出字符,直到它得到一个空字符。所以在这种情况下,很多未初始化的内存╠字符直到\0在内存中找到字符。


似乎是未初始化的data变量在上一次运行时写入文件,现在按原样读回。如果读取不成功,它可能会放置一个 \0。在 data 的第一个位置char数组,在那种情况下不会有输出。

我用一个空的输入文件运行程序并添加了一个学生。这存储在文本文件中(使用 Visual Studio 的文本编辑器查看):

REGNUM123   Pete           ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ    00  00  PROC432

在这种情况下 Ì , 或带有严肃字符的拉丁文大写字母 i 是 0xCC Unicode and multiple other encodings 中的十六进制值.添加学生时,写入的字符数不一定等于 data 中的字符数。数组(在这种情况下为 15)。它们一直写到 \0找到了。

关于c++ - 我的程序控制台输出中出现未知的 ascii 代码字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50117087/

相关文章:

c++ - SFML OpenGL 绘图文本

c++ - Objective C 类方法 == C++ 构造函数?

c++ - 当库更新时,我应该重新编译程序吗

c++ - visual studio 继续多行注释

visual-studio - 如何使用 Visual Studio 2015 和 Windows 10 模拟传感器?

visual-studio - Less:评估函数 data-uri 时出错:找不到模块 './types.json'

c++ - 继承链中间的静态转换

c++ - 防止静态初始化顺序 "fiasco",C++

c# - VS2012 和 VS2015 浮点/ double 转换的区别

c++ - CMake 不会为简单的测试程序生成 vc pdb