2012-12-19 103 views
1

這是用於實現脫機消息傳遞系統的C++程序。首先,用戶必須通過選擇用戶名和密碼來註冊帳戶。然後他們可以向其他用戶發送和接收消息。文件處理類對象

我寫了程序,但似乎有一些邏輯錯誤。

我在註冊時將用戶詳細信息寫入文件電子郵件。數據作爲對象寫入文件。

假設我註冊了3個賬戶。但我無法登錄IE。我無法驗證登錄並將控制權交給signin();

要排除錯誤,我創建了一個顯示文件電子郵件內容的print()函數。

我已經把此之前登錄驗證部分,看看數據記錄存在

但是沒有被顯示的記錄。

我不能測試其他部分的代碼,直到我解決這個問題。

部分代碼,我認爲是錯誤的可能原因:

#include<fstream.h> 
#include<conio.h> 
#include<string.h> 
#include<process.h> 
#include<stdio.h> 

int user;   // to keep a track of no. of users 

class mail 
{ 
public: 
    char un[25];    // user name 
    char pd[25];    // passsword 
    int ino;     // no. of inbox messages 
    int sno;     //no.of sent messags 
    char isbj[25][25]; 
    char ssbj[25][25]; 
    char imsg[25][50]; 
    char smsg[25][50];  // character array to store inbox and sent messages respecvly 
    void reg();    // func to sign up 
    void signin(); 
    mail() 
    { 
    } 
    ~mail() 
    { 
    } 
}; 

void print(); 
void print()      // for testing purpose. retrieving data in the file 
{ 
    mail o; 
    cout<<"\nRunning print func();"; 

    ifstream filein; 
    filein.open("email",ios::in|ios::binary); 
    if(!filein) 
    { 
     cout<<"Unable to open file to read\n"; 
    } 
    else 
    { 
     filein.seekg(0); 
     filein.read((char *)&o,sizeof(o)); 
     while(filein) 
     { 
      puts(o.un); 
      puts(o.pd); 
      cout<<o.ino<<" "<<o.sno; 
      filein.read((char *)&o,sizeof(o)); 
     } 
     getch(); 
    } 
} 


void mail::reg()      // sign up function 
{ 
    clrscr(); 
    cout<<"Enter user name (enter unique name)\n"; 
    gets(un); 
    cout<<"Enter password\n"; 
    gets(pd); 
    ofstream filout; 
    filout.open("email",ios::app|ios::binary); 
    if(!filout) 
    { 
     cout<<"cannot open file\n"; 
    } 
    else 
    { 
     ino=-1; 
     sno=-1;     // initialising no. of sent and inbox msgs as 0 on regtrn 

     filout.write((char *)&this,sizeof(this)); 
     filout.close(); 
    } 
    cout<<"You are now registered. Plz login\n"; 
    getch(); 

}  // end of sign up or register func 



void mail::signin() 
{ 
    . 
     . 
     . 
} // end of signin 




void screen(void); 
void signup(void); 


void main() 
{ 
    clrscr(); 
    char usrnm[25],pwd[25],s[25],c; 
    int i; 
m: 
    clrscr(); 
    screen(); 
    gotoxy(30,4); 
    c=getch(); 
    if(c=='i'||c=='I')    //if user opts to sign in 
    { 
     gotoxy(62,11); 
     gets(usrnm); 
     gotoxy(62,13); 
     for(i=0;i< 25;i++)     // getting password 
     { 
      if(i < 0) 
       i=0; 
      pwd[i]=getch(); 
      if(pwd[i]=='\b') 
      { 
       i-=2; 
       cout<<"\b \b"; 
       continue; 
      } 

      if(pwd[i]=='\r') 
       break; 
      cout<<"*"; 
     } 
     pwd[i]='\0'; 
     clrscr();    // pwd now contains password for sign in 

     print(); 

     int f; 
     {      // local block 

      mail obj; 
      ifstream filein; 
      filein.open("email",ios::in|ios::binary); // to read records from file and authenticating the user 
      if(!filein) 
      { 
       cout<<"Unable to open file to read\n"; 
      } 
      else 
      { 
       filein.seekg(0); 
       filein.read((char *)&obj,sizeof(obj)); 
       while(filein) 
       { 
        if(strcmp(usrnm,obj.un)==0) 
        { 
         f=1; 
         if(strcmp(pwd,obj.pd)==0) 
         { 
          obj.signin();   // user authenticated. now transfering control 
          break; 
         } 
         else 
         { 
          cout<<"wrong password\n"; 
          getch(); 
          break; 
         } 
        } // end of if 
        else 
        { 
        } 
        filein.read((char *)&obj,sizeof(obj)); 
       }    // end of while 
       filein.close(); 
      } // end of else 

     } 


     if(f==0) 
      cout<<"Wrong username or password\n"; 
     getch(); 
     goto m; 
    } 
    else if(c=='s'||c=='S')  // user opts to sign up 
    { 
     signup(); 
     getch(); 
     goto m; 
    } 
    else if(c=='e'||c=='E')  // user choose to exit 
     exit(0); 
    else 
    { 
     clrscr(); 
     cout<<"you have entered a wrong key. please check and try again\n"; 
     getch(); 
     goto m; 
    } 

} //end of main 


void signup()     // sign up function 
{ 
    int t; 
    clrscr(); 
    { 
     mail obj; 
     ifstream filin; 
     filin.open("user.txt",ios::ate);  // a file to store the no. of currently registered user. THIS IS FOR FUTURE USE 
     if(!filin) 
     { 
      cout<<"cannot open file\n"; 
     } 
     else 
     { 
      filin.seekg(0); 
      filin>>t; 
     } 
     filin.close(); 
     user=t; 
     ++user;      // incrementing no. users 

     ofstream filout; 
     filout.open("user.txt",ios::out); 
     if(!filout) 
     { 
      cout<<"cannot open file\n"; 
     } 
     else 
     { 
      filout.seekp(0); 
      filout<<user;     //Updating user 
     } 
     filin.close(); 
     obj.reg(); 

    } // end of block 

    return; 

}  // end of sign up 



void screen()    // function for login screen 
{ 
    . 
     . 
} 

全碼:

#include<fstream.h> 
#include<conio.h> 
#include<string.h> 
#include<process.h> 
#include<stdio.h> 

int user;   // to keep a track of no. of users 

class mail 
{ 
public: 
    char un[25];    // user name 
    char pd[25];    // passsword 
    int ino;     // no. of inbox messages 
    int sno;     //no.of sent messags 
    char isbj[25][25]; 
    char ssbj[25][25]; 
    char imsg[25][50]; 
    char smsg[25][50];  // character array to store inbox and sent messages respecvly 
    void reg();    // func to sign up 
    void signin(); 
    mail() 
    { 
    } 
    ~mail() 
    { 
    } 
}; 

void print(); 
void print()      // for testing. retrieving data in the file 
{ 
    mail o; 
    cout<<"\nRunning print func();"; 

    ifstream filein; 
    filein.open("email",ios::in|ios::binary); 
    if(!filein) 
    { 
     cout<<"Unable to open file to read\n"; 
    } 
    else 
    { 
     filein.seekg(0); 
     filein.read((char *)&o,sizeof(o)); 
     while(filein) 
     { 
      puts(o.un); 
      puts(o.pd); 
      cout<<o.ino<<" "<<o.sno; 
      filein.read((char *)&o,sizeof(o)); 
     } 
     getch(); 
    } 
} 


void mail::reg()      // sign up function 
{ 
    clrscr(); 
    cout<<"Enter user name (enter unique name)\n"; 
    gets(un); 
    cout<<"Enter password\n"; 
    gets(pd); 
    ofstream filout; 
    filout.open("email",ios::app|ios::binary); 
    if(!filout) 
    { 
     cout<<"cannot open file\n"; 
    } 
    else 
    { 
     ino=-1; 
     sno=-1;     // initialising no. of sent and inbox msgs as 0 on regtrn 

     filout.write((char *)&this,sizeof(this)); 
     filout.close(); 
    } 
    cout<<"You are now registered. Plz login\n"; 
    getch(); 

}  // end of sign up or register func 



void mail::signin() 
{ 
    int f,i; 
    char subj[25],message[50],usrnm[25]; // temporary varibles to store subject,message and username 

start: 
    clrscr(); 
    cout<<"1.compose \n2.inbox \n3.Sent\n4.logout"; 
    cout<<"\nEnter ur choice\n"; 
    int ch; 
    cin>>ch; 

    switch(ch) 
    { 

    case 1:          // switch for compose message 
     cout<<"enter username\n"; 
     gets(usrnm); 
     {   // beginning of local loop 
      mail temp; 

      ifstream filein; 
      filein.open("email",ios::in|ios::binary); 
      if(!filein) 
      { 
       cout<<"Unable to open file to read\n"; 
      } 
      else 
      { 
       filein.read((char *)&temp,sizeof(temp)); 
       while(filein) 
       { 
        if(strcmp(usrnm,temp.un)==0) 
        { 
         cout<<" User name valid\n"; 
         f=1; 
         cout<<"Enter subject\n"; 
         gets(subj); 
         cout<<"Enter message\n"; 
         gets(message); 
         temp.ino++; 
         strcpy(temp.isbj[temp.ino],subj); 
         strcpy(temp.imsg[temp.ino],message); // copying message to recepient inbox 

         sno++; 
         strcpy(ssbj[sno],subj); 
         strcpy(smsg[sno],message); // copying messages to sender sent folder 

         cout<<"Your message has been sent successfully\n"; 
         break; 
        } 
        else  // to avoid if else mismatch 
        { 
        } 
        filein.read((char *)&temp,sizeof(temp)); 
       }    // end of while 
      } 
      filein.close(); 


      mail temp1; 

      long pos;     // below code modifies current users object 
      fstream fio("email",ios::in|ios::out|ios::binary); 
      pos=fio.tellg(); 
      fio.read((char*)&temp1,sizeof(temp1)); 
      while(fio) 
      { 
       if(strcmp(un,temp1.un)==0) 
       { 
        fio.seekg(pos); 
        fio.write((char*)&this,sizeof(this)); 
        break; 
       } 
       pos=fio.tellg(); 
       fio.read((char*)&temp1,sizeof(temp1)); 
      } //end of while 
      fio.close(); 

      mail temp2;    // below code modifies recipients object 

      fio.open("email",ios::in|ios::out|ios::binary); 
      pos=fio.tellg(); 
      fio.read((char*)&temp2,sizeof(temp2)); 
      while(fio) 
      { 
       if(strcmp(temp.un,temp2.un)==0) 
       { 
        fio.seekg(pos); 
        fio.write((char*)&temp,sizeof(temp)); 
        break; 
       } 
       pos=fio.tellg(); 
       fio.read((char*)&temp2,sizeof(temp2)); 
      } // end of while 
      fio.close(); 
     } // end of block 

     if(f==0) 
      cout<<"invalid user. Please enter a valid user name\n"; 
     getch(); 
     break; 

    case 2: 
     if(ino<0) 
      cout<<"There are no messages in your inbox\n"; 
     else 
     { 
      for(i=0;i<=ino;i++) 
      { 
       cout<<"\nMessage "<<i+1<<"\n"; // printing inbox messages 
       puts(isbj[i]); 
       puts(imsg[i]); 
      } 
     } 
     getch(); 
     break; 

    case 3: 
     if(sno<0) 
      cout<<"There are no sent messages\n"; 
     else 
     { 
      for(i=0;i<=sno;i++) 
      { 
       cout<<"\nMessage "<<i+1<<"\n";  // printing sent messages 
       puts(ssbj[i]); 
       puts(smsg[i]); 
      } 
     } 
     getch(); 
     break; 

    case 4: 
     cout<<"Logout sucess\n"; 
     return; 

    default : cout<<"wrong choice\n"; 

    } // end of switch 

    goto start; 

} // end of signin 




void screen(void); 
void signup(void); 


void main() 
{ 
    clrscr(); 
    char usrnm[25],pwd[25],s[25],c; 
    int i; 
m: 
    clrscr(); 
    screen(); 
    gotoxy(30,4); 
    c=getch(); 
    if(c=='i'||c=='I')    //if user opts to sign in 
    { 
     gotoxy(62,11); 
     gets(usrnm); 
     gotoxy(62,13); 
     for(i=0;i< 25;i++)     // getting password 
     { 
      if(i < 0) 
       i=0; 
      pwd[i]=getch(); 
      if(pwd[i]=='\b') 
      { 
       i-=2; 
       cout<<"\b \b"; 
       continue; 
      } 

      if(pwd[i]=='\r') 
       break; 
      cout<<"*"; 
     } 
     pwd[i]='\0'; 
     clrscr();    // pwd now contains password for sign in 

     print(); 

     int f; 
     {      // local block 

      mail obj; 
      ifstream filein; 
      filein.open("email",ios::in|ios::binary); // to read records from file and authenticating the user 
      if(!filein) 
      { 
       cout<<"Unable to open file to read\n"; 
      } 
      else 
      { 
       filein.seekg(0); 
       filein.read((char *)&obj,sizeof(obj)); 
       while(filein) 
       { 
        if(strcmp(usrnm,obj.un)==0) 
        { 
         f=1; 
         if(strcmp(pwd,obj.pd)==0) 
         { 
          obj.signin();   // user authenticated. now transfering control 
          break; 
         } 
         else 
         { 
          cout<<"wrong password\n"; 
          getch(); 
          break; 
         } 
        } // end of if 
        else 
        { 
        } 
        filein.read((char *)&obj,sizeof(obj)); 
       }    // end of while 
       filein.close(); 
      } // end of else 

     } 


     if(f==0) 
      cout<<"Wrong username or password\n"; 
     getch(); 
     goto m; 
    } 
    else if(c=='s'||c=='S')  // user opts to sign up 
    { 
     signup(); 
     getch(); 
     goto m; 
    } 
    else if(c=='e'||c=='E')  // user choose to exit 
     exit(0); 
    else 
    { 
     clrscr(); 
     cout<<"you have entered a wrong key. please check and try again\n"; 
     getch(); 
     goto m; 
    } 

} //end of main 


void signup()     // sign up function 
{ 
    int t; 
    clrscr(); 
    { 
     mail obj; 
     ifstream filin; 
     filin.open("user.txt",ios::ate);  // a file to store the no. of currently registered user. THIS IS FOR FUTURE USE 
     if(!filin) 
     { 
      cout<<"cannot open file\n"; 
     } 
     else 
     { 
      filin.seekg(0); 
      filin>>t; 
     } 
     filin.close(); 
     user=t; 
     ++user;      // incrementing no. users 

     ofstream filout; 
     filout.open("user.txt",ios::out); 
     if(!filout) 
     { 
      cout<<"cannot open file\n"; 
     } 
     else 
     { 
      filout.seekp(0); 
      filout<<user;     //Updating user 
     } 
     filin.close(); 
     obj.reg(); 

    } // end of block 

    return; 

}  // end of sign up 



void screen()    // function for login screen 
{ 
    int i; 
    for(i=0;i<80;i++) 
     cout<<"*"; 
    cout<<" SuperMail        New to SuperMial ? Create an Account  "; 
    for(i=0;i<80;i++) 
     cout<<"*"; 
    cout<<" Press S To Sign up     Already have a account Press I to sign in"; 
    for(i=0;i<80;i++) 
     cout<<"-"; 
    gotoxy(1,6); 
    cout<<"\n\n\n\n Super mail is built on the idea that \n it can be more intuitive,efficient and \n useful and may be even fun:\n\n Lots of space\n\n Less spam"; 
    gotoxy(1,22); 
    for(i=0;i<80;i++) 
     cout<<"-"; 
    gotoxy(32,23); 
    cout<<"PRESS E TO EXIT"; 
    gotoxy(1,24); 
    for(i=0;i<80;i++) 
     cout<<"-"; 
    gotoxy(50,10); 
    cout<<"-------------------------------"; 
    gotoxy(50,11); 
    cout<<"| USERNAME :     |"; 
    gotoxy(50,12); 
    cout<<"-------------------------------"; 
    gotoxy(50,13); 
    cout<<"| PASSWORD :     |"; 
    gotoxy(50,14); 
    cout<<"-------------------------------"; 
} 
+0

爲什麼你不使用c + +字符串而不是頭痛的char數組會給你?我不太理解你使用舊C概念的方法 –

+1

嘗試*解決你的問題*,發佈*最小代碼*並省略與問題無關的信息。就目前而言,你的問題和發佈的代碼太長了。 –

+0

啊! 「開始了;」當我在C或C++代碼中看到類似這樣的東西時,我只是覺得跑得很遠...... – kebs

回答

1

我沒有經歷過所有的代碼讀取,但一個問題是

filout.write((char *)&this,sizeof(this)); 

this是一個指針,所以上面的代碼寫你的類實例的地址歸檔。你可以使用

filout.write((char*)this,sizeof(*this)); 

我預計這也將寫出來你不希望一些數據寫出所有類成員/需要如V-表的地址。您最好明確地堅持你關心的班級成員。

+0

感謝讓我再次編譯它 – rafi343e

+0

非常感謝你。有用。現在我可以登錄併發送消息。我還沒有完全測試過,但我想先謝謝你。再次感謝 – rafi343e

+0

很高興聽到它的作品;感謝您接受我的答案。對於未來的問題,您能否考慮您以前收到的各種反饋信息?這裏的人通常非常有幫助,但當你問儘可能小而特定的問題時,你會得到最好的迴應。 – simonc