Strings not listing correctly and file whith special characters not letters



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
 #include <iostream>
#include <cstdio>
#include <conio.h>


using namespace std;
main()
{
    FILE *fp, *ft;
    char another, choice;
    

    struct student
    {
        int id;
        string name;
        int age;
        char gender[7];
        string address;
        string blood_group;
		string doc_name;
		int amount;
		char op_tr[1];
        
    };
    
    
    struct student e;
    int xid;
    int recsize;
    int ifound;

    fp=fopen("users.txt","rb+");

    if (fp == NULL)
    {
        fp = fopen("users.txt","wb+");

        if (fp==NULL)
        {
            puts("Cannot open file");
            return 0;
        }
    }


    recsize = sizeof(e);

    while(1)
    {
        system("cls");

        cout << "\t\t====== STUDENT DATABASE MANAGEMENT SYSTEM ======";
        cout <<"\n\n                                          ";
        cout << "\n\n";
        cout << "\n \t\t\t 1. Add    Records";
        cout << "\n \t\t\t 2. List   Records";
        cout << "\n \t\t\t 3. Search Record";
        cout << "\n \t\t\t 4. Modify Records";
        cout << "\n \t\t\t 5. Delete Records";
        cout << "\n \t\t\t 6. Exit   Program";
        cout << "\n\n";
        cout << "\t\t\t Select Your Choice :=> ";
        fflush(stdin);
        choice = getche();
        switch(choice)
        {
        case '1' :
            fseek(fp,0,SEEK_END);
            another ='Y';
            while(another == 'Y' || another == 'y')
            {
                cout << "\nEnter ID : ";
                cin >> e.id;
                cin.ignore();
                cout <<"\nEnter Name: ";
                getline(cin,e.name);
                cout <<"\nEnter Age: ";
                cin>>e.age;
                cin.ignore();
                cout <<"\nEnter Blood Group: ";
                getline(cin,e.blood_group);
                cin.ignore();
                cout <<"\nEnter Address: ";
                getline(cin,e.address);
				cin.ignore();
                cout <<"\nEnter Doctor Name: ";
                getline(cin,e.doc_name);
                cout <<"\nEnter Amount Paid: ";
                cin>>e.amount;
                cout <<"\nEnter Operation/Treatment: ";
                cin>>e.op_tr;
                
                fwrite(&e,recsize,1,fp);
                cout << "\n Add Another Record (Y/N) ";
                fflush(stdin);
                another = getchar();
            }
            break;
            
        case '2':
            system("cls");
            rewind(fp);
            cout << "=== View the Records in the Database ===";
            cout << "\n";
            while (fread(&e,recsize,1,fp) == 1)
            {
                cout << "\n\n";
                cout << e.id << endl;
                cout << e.name << endl;
                cout << e.age <<  endl;
                cout << e.address << endl;
                cout << e.blood_group<< endl;
                cout << e.amount << endl;
                cout << e.op_tr << endl;
				cout << "\n ================================";
            }
            cout << "\n\n";
            system("pause");
            break;

default:
cout <<"BLAA"<<endl;
}


In this program i am saving the input in file from case 1 and output will be done from case 2 but the strings are same in all three records i entered using case 1 what's the problem and when i open the file user.txt it shows special characters not letters.
Last edited on
Of course it shows special characters which represent the object that you are writing to the file. What other characters do you expect it to show?

Note if the reading and writing works for you, that's fine but this method is not recommended way of writing objects to file due to endianess and other factors that make this method not portable across platforms. There are better ways in c++ where you can overload the std::istream >> and the std::ostream << operators to allow for reading an object in a specific manner.

For example you can define the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Student {
    string name, address, blood_group, doc_name;
    int id, age, amount;
    char gender[7], char op_tr;
};

std::istream& operator >> (std::istream& iss, Student& stu) {
    return iss >> stu.name >> stu.address >> stu.blood_group
               >> stu.doc_name >> stu.id >> stu.age >> stu.amount
               >> stu.gender >> stu.op_tr;
}

int main() {
    Student s;
    std::cin >> s;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.