2014-04-23 164 views
0

我在学生管理系统的C++中有这个程序,除了一个地方,我尝试根据他的编号删除一个学生,一切工作正常。 它应该做的:问卷数后搜索记录,并删除它 这是什么做的:它删除所有不匹配的卷号学生管理系统C++

这里的其他记录是我的代码:

#include <iostream> 

    #include <cstdio> 

    #include <cstring> 

    #include <cstdlib> 

    #include <conio.h> 

    #include <iomanip> 



using namespace std; 
int main() { 

    FILE *fp, *ft; 

    char another, choice; 



struct student { 

     char first_name[50], last_name[50]; 
     int roll_num; //new code added 
     char course[100]; 
     int section; 
    }; 

    struct student e; 
    char xfirst_name[50], xlast_name[50]; 
    int xroll_num ; // new code added 
    long int recsize; 

    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 INFORMATION SYSTEM ======"; 
    cout <<"\n\n           "; 
    cout << "\n\n"; 
    cout<<" \n\t\t\t======================"; 
    cout << "\n \t\t\t 1. Add Records"; 
    cout << "\n \t\t\t 2. List Records"; 
    cout << "\n \t\t\t 3. Modify Records"; 
    cout << "\n \t\t\t 4. Delete Records"; 
    cout << "\n \t\t\t 5. Exit Program"; 
    cout<<" \n\t\t\t======================"; 
    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') 
      { 
        system("cls"); 
       cout << "Enter the First Name : "; 
       cin >> e.first_name; 
       cout << "Enter the Last Name : "; 
       cin >> e.last_name; 
       cout << "Enter the Course : "; 
       cin >> e.course; 
       cout << "Enter the Section : "; 
       cin >> e.section; 
       cout << "Enter the roll number :"; 
       cin >> e.roll_num; 
       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"; 
      cout <<"\nName  :: " <<e.first_name <<" "<<e.last_name; 
      //cout << "\n"; 
      cout <<"\nRoll Number :: " << e.roll_num ; 
      cout <<"\nCourse :: " <<e.course ; 
      cout <<"\nSection :: "<<e.section; 
      } 
      cout << "\n\n"; 
      system("pause"); 
      break; 

     case '3' : 
      system("cls"); 
      another = 'Y'; 
      while (another == 'Y'|| another == 'y') 
      { 
     //  cout << "\n Enter the last name of the student : "; 
       cout << "\n Enter the Roll number of the student : "; 
       cin >> xroll_num; 

      rewind(fp); 
      while (fread(&e,recsize,1,fp) == 1) 
      { 
       //if (strcmp(e.last_name,xlast_name) == 0) 
       if(e.roll_num == xroll_num) 
       { 
       cout << "Enter the new Firt Name : "; 
       cin >> e.first_name; 
       cout << "Enter the new Last Name : "; 
       cin >> e.last_name; 
       cout << "Enter the new Roll Number : "; 
       cin >> e.roll_num; 
       cout << "Enter the new Course : "; 
       cin >> e.course; 
       cout << "Enter the new Section : "; 
       cin >> e.section; 
       fseek(fp, - recsize, SEEK_CUR); 
       fwrite(&e,recsize,1,fp); 
       break; 
       } 
       else 
       cout<<"record not found"; 
      } 
      cout << "\n Modify Another Record (Y/N) "; 
       fflush(stdin); 
       another = getchar(); 
      } 
      break; 


     case '4': 
     system("cls"); 
      another = 'Y'; 
      while (another == 'Y'|| another == 'y') 
      { 
      // cout << "\n Enter the last name of the student to delete : "; 
       cout <<"\n Enter the roll number of the student to delete : "; 
       cin >> xroll_num; 

       ft = fopen("temp.dat", "wb"); 

       rewind(fp); 
       while (fread (&e, recsize,1,fp) == 1) 

       // if (strcmp(e.last_name,xlast_name) != 0) 
        if(e.roll_num == xroll_num) 
       { 
        fwrite(&e,recsize,1,ft); 
       } 
       fclose(fp); 
       fclose(ft); 
       remove("users.txt"); 
       rename("temp.dat","users.txt"); 

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

       cout << "\n Delete Another Record (Y/N) "; 
       fflush(stdin); 
       another = getchar(); 
       } 

       break; 

       case '5': 
       fclose(fp); 
       cout << "\n\n"; 
       cout << "\t\t  THANK YOU FOR USING THIS SOFTWARE"; 
       cout << "\n\n"; 
       exit(0); 
      } 
      } 
    system("pause"); 
return 0; 
} 
+0

你能在控制台上打印“e.roll_num”的值吗 – jaipster

+0

你调用这个C++的任何特定原因(但是用于'cout')? –

回答

1

它删除所有不使用使得f匹配的卷号

那么你只写匹配的卷号到临时文件中的记录,然后其他记录ILE覆盖users.txt文件

if (e.roll_num == xroll_num) { 
    fwrite(&e, recsize, 1, ft); 
} 

我想你真正想要做的是

if (e.roll_num != xroll_num) { 
    fwrite(&e, recsize, 1, ft); 
} 

你或许应该好好读读C++的I/O教程,因为你的代码大多是C.考虑写您的student结构作为简单的文本,而不是批量写入文件。

+0

你可以建议我任何教程,我可以将所有值保存到行列顺序或适当的Excel文件中? – user3562802

+0

@ user3562802 csv文件非常简单易用,可以在excel中打开。 – user657267