fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. #define SIZE 50
  8. char record[SIZE];
  9.  
  10. class Student{
  11. private:
  12. char id[10];
  13. char name[20];
  14. char address[20];
  15.  
  16. public:
  17. void setData(){
  18. cout << "enter your id:" << endl;
  19. cin >> id;
  20. cout << "enter your name:" << endl;
  21. cin >> name;
  22. cout << "enter your address:" << endl;
  23. cin >> address;
  24. }
  25.  
  26. void printData(){
  27. cout << "id: " << id << " name: " << name << " address: " << address << endl;
  28. }
  29.  
  30. void pack(){
  31. strcpy(record, id);
  32. strcat(record, "|");
  33. strcat(record, name);
  34. strcat(record, "|");
  35. strcat(record, address);
  36. while(strlen(record) <= SIZE -1){
  37. strcat(record, "#");
  38. }
  39. strcat(record, "\n");
  40. }
  41.  
  42. void unpack(){
  43. char* p;
  44. p=strtok(record,"|");
  45. strcpy(id,p);
  46. p=strtok(NULL,"|");
  47. strcpy(name,p);
  48. p=strtok(NULL,"#");
  49. strcpy(address,p);
  50. }
  51.  
  52. void insert(){
  53. fstream file;
  54. file.open("student.txt", ios::out | ios::app);
  55. file << record;
  56.  
  57. file.close();
  58. }
  59.  
  60. void display(){
  61. fstream file;
  62. file.open("student.txt", ios::in);
  63. while(file.getline(record, SIZE+1, '\n')){
  64. unpack();
  65. printData();
  66. }
  67.  
  68. file.close();
  69. }
  70. void search(char* key){
  71. ifstream Infile("student.txt");
  72. while(Infile.getline(record, SIZE+1, '\n')){
  73. unpack();
  74. if(strcmp(key, id) == 0){
  75.  
  76. printData();
  77. }
  78.  
  79. }
  80. Infile.close();
  81. }
  82.  
  83. void deleteD(char *key){
  84. fstream in_file;
  85. fstream out_file;
  86.  
  87. in_file.open("student.txt",ios::in);
  88. out_file.open("temp.txt",ios::out);
  89.  
  90. while(!in_file.eof()){
  91. in_file.getline(record,SIZE+1,'\n');
  92. if(in_file.fail()) break;
  93. unpack();
  94. if(strcmp(key,id)==0){
  95. continue;
  96. }
  97. pack();
  98. out_file<<record;
  99. }
  100. in_file.close();
  101. out_file.close();
  102. remove("student.txt");
  103. rename("temp.txt","student.txt");
  104. }
  105.  
  106. void update(char *key){
  107. fstream in_file;
  108. fstream out_file;
  109.  
  110. in_file.open("student.txt",ios::in);
  111. out_file.open("temp.txt",ios::out);
  112.  
  113. while(!in_file.eof()){
  114. in_file.getline(record,SIZE+1,'\n');
  115. if(in_file.fail()) break;
  116. unpack();
  117. if(strcmp(key,id)==0){
  118. setData();
  119. }
  120. pack();
  121. out_file<<record;
  122. }
  123. in_file.close();
  124. out_file.close();
  125. remove("student.txt");
  126. rename("temp.txt","student.txt");
  127. }
  128.  
  129. };
  130.  
  131.  
  132. int main()
  133. {
  134. Student obj;
  135. //st.setData();
  136. //st.pack();
  137. //st.insert();
  138. //st.unpack();
  139. //st.printData();
  140. //st.display();
  141. //st.search();
  142. //st.update("56565");
  143.  
  144. return 0;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty