在找到总计并将其插入C ++文件中的同时获取垃圾值

将随机字符或值插入文件,同时在void report :: getdata()函数中查找总数。

将数据读入字符数组,因为它们可以插入缓冲区。

我尝试将m1,m2,m3,m4,m5转换为整数并将其转换回字符,以便可以将它们复制到缓冲区中并插入文件中。 有人可以告诉我我在做什么错。

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<stdio.h>
#define SIZE 55
char buffer[SIZE + 1];
class report
{
public:
    int a,b,c,d,e,s,avg;
    char rollno[100];
    char name[100];
    char m1[10];
    char m2[10];
    char m3[10];
    char m4[10];
    char m5[10];
    char grade[10];
    char average;
    char total[100];
    void pack();
    void unpack();
    void getdata();
    void putdata();
    void insert();
    void create();
    void sort();
    void search();
    void update();
    void display();
    void failures();
    void modify(char *key);
    void search(char *key);

};

void report::getdata()
{

    cout << "ENTER THE ROLL NO OF THE STUDENT:\t";
    cin >> rollno;
    cout << "ENTER THE NAME OF THE STUDENT:\t";
    cin >> name;
    cout << "ENTER THE MARKS OF THE FOLLOWING SUBJECTS OUT OF 100\n";
    cout << "OPERATION RESEARCH:\t";
    cin >> m1[0];
    cout << "CRYPTOGRAPHY:\t";
    cin >> m2[0];
    cout << "OPERATING SYSTEMS:\t";
    cin >> m3[0];
    cout << "FILE STRUCTURE:\t";
    cin >> m4[0];
    cout << "SOFTWARE TESTING:\t";
    cin >> m5[0];
    a = m1[0] - '0';
    b = m2[0] - '0';
    c = m3[0] - '0';
    d = m4[0] - '0';
    e = m5[0] - '0';
    s = a + b + c + d + e;
    total[0] = s + '0';

}

void report::putdata()
{
    cout << rollno << "\t" << name << "\t" << m1 << "\t" << m2 << "\t" << m3
            << "\t" << m4 << "\t" << m5 << "\t" << total << endl;

}
void report::pack()
{
    strcpy(buffer,rollno);
    strcat(buffer,"|");
    strcat(buffer,name);
    strcat(buffer,m1);
    strcat(buffer,m2);
    strcat(buffer,m3);
    strcat(buffer,m4);
    strcat(buffer,m5);
    strcat(buffer,total);
    strcat(buffer,"\n");
}
void report::unpack()
{
    char *p;
    p = strtok(buffer,"|");
    strcpy(rollno,p);
    p = strtok(NULL,"|");
    strcpy(name,"|");
    strcpy(m1,"|");
    strcpy(m2,"| ");
    strcpy(m3,"|");
    strcpy(m4,"|");
    strcpy(m5,"|");
    strcpy(total,p);

}

void report::insert()
{
    getdata();
    pack();
    ofstream fout("test.txt",ios::app);
    fout << buffer;
    fout.close();
}
void report::display()
{
    ifstream fin("test.txt");
    while (!fin.eof())
    {
        fin.getline(buffer,SIZE + 1,'\n');
        if (fin.fail())
            break;
        unpack();
        putdata();
    }
    fin.close();
}
void report::search(char *key)
{
    ifstream fin("test.txt");
    int count = 0;
    while (!fin.eof())
    {
        fin.getline(buffer,'\n');
        if (fin.fail())
            break;
        unpack();
        if (strcmp(rollno,key) == 0)
        {
            putdata();
            count++;
        }
    }
    cout << "TOTAL RECORDS FOUND:" << count << endl;
    fin.close();
}
void report::modify(char *key)
{
    ifstream fin("test.txt");
    ofstream fout("temp.txt");
    int count = 0;
    while (!fin.eof())
    {
        fin.getline(buffer,key) == 0)
        {
            getdata();
            count++;
        }
        pack();
        fout << buffer;
    }
    if (count == 0)
        cout << "ROLL NO NOT FOUND!!" << endl;
    else
        cout << "MODIFIED!!" << endl;
    fin.close();
    fout.close();
    remove("test.txt");
    rename("temp.txt","test.txt");
}

int main()
{
    int choice;
    int n;
    report r;
    char key[20];
    clrscr();

    while (1)
    {

        cout << "--------------------\n" << endl;
        cout << "1.MAKE REPORT CARD\n" << "2.UPDATE DetaILS\n"
                << "3.SORT NAMES\n" << "4.SEARCH A NAME\n" << "5.UPDATE GRADE\n"
                << "6.VIEW REPORT CARD\n" << "7.LIST OF FAILURES\n" << "8.EXIT"
                << endl;
        cout << "\n--------------------\n\n" << endl;
        cout << "ENTER YOUR CHOICE:\t" << endl;
        cin >> choice;

        switch (choice)
        {
            case 1:
                r.insert();

                cout << "VALUES INSERTED\n" << endl;
                break;

            case 2:
                cout << "ENTER THE ROLL NO TO MODIFY:\t" << endl;
                cin >> key;
                r.modify(key);
                break;

            case 3:
                cout << "example 3" << endl;
                break;

            case 4:
                cout << "ENTER THE ROLL NO:\t" << endl;
                cin >> key;
                r.search(key);

                break;

            case 5:
                cout << "example 5" << endl;
                break;

            case 6:
                cout << "REPORT CARD DetaILS\n" << endl;
                r.display();
                break;

            case 7:
                cout << "example 7" << endl;
                break;

            case 8:
                exit(0);
                break;
            default:
                return 0;
        }
    }
}
iCMS 回答:在找到总计并将其插入C ++文件中的同时获取垃圾值

解决问题的最佳方法是使用二进制文件存储数据。

首先,删除pack()unpack()方法。我们将不需要那些。

void report::insert()
{
    r.getdata();
    ofstream fout("test.dat",ios::app|ios::binary);
    fout.write((char*)&r,sizeof(r));
    fout.close();
}

void report::search(char *key)
{
    ifstream fin("test.dat",ios::binary);
    int count = 0;
    while (!fin.eof())
    {
        fin.read((char*)&r,sizeof(r));        
        if (fin.fail())
            break;
        if (strcmp(r.rollno,key) == 0)
        {
            r.putdata();
            count++;
        }
    }
    cout << "TOTAL RECORDS FOUND:" << count << endl;
    fin.close();
}

void report::display()
{
    ifstream fin("test.dat",ios::binary);
    while (!fin.eof())
    {
        fin.read((char*)&r,sizeof(r));
        if (fin.fail())
            break;
        r.putdata();
    }
    fin.close();
}
本文链接:https://www.f2er.com/2238443.html

大家都在问