2013-12-18 41 views
0

我一直在思考如何做到這一點「保存到文件」的事情了一夜,但似乎就在我身邊運氣wasnt ..。 我對此很陌生,而且我想學習,那就是所有。 我來自保加利亞,對於那些想知道什麼是「edinen」的人,這是您的公民號碼,作爲該國的成員。 (我仍然不知道如何解釋這個...)C++保存記錄到文件

這裏是我有什麼 (我已經包括了「fstream的」,但我仍然不知道如何使用它!

#include <iostream> 
#include <stdlib.h> 
#include <fstream> 
using namespace std; 
#define n 30 
int num=0; 
struct uslugi 
{ 
    char name[30]; 
    char surname[30]; 
    char lastname[30]; 
    char illness[30]; 
    long int edinen; 
}grupa[n]; 
void add_record(); 
void show_record(); 
void search_record(); 
void remove_record(); 
void add_record() // FUNCTION - ADD RECORD(S) 
{ 
    system("title Add Record"); 
    int br; 
    cout<<"\n How many pacients do you want to enter?"; 
    cout<<"\n >> "; 
    cin>>br; 
    for(int i=num;i<num+br;i++) 
    { 
     cout<<"\n \t\t ENTERING DATA FOR PACIENT NUMBER - "<<i+1<<endl; 
     cout<<"\n Name:"; 
     cout<<"\n >> "; 
     cin>>grupa[i].Name; 
     cout<<"\n Surname:"; 
     cout<<"\n >> "; 
     cin>>grupa[i].surname; 
     cout<<"\n Lastname:"; 
     cout<<"\n >> "; 
     cin>>grupa[i].lastname; 
     cout<<"\n edinen:"; 
     cout<<"\n >> "; 
     cin>>grupa[i].edinen; 
     cout<<"\n Ill from:"; 
     cout<<"\n >> "; 
     cin>>grupa[i].illness; 
    } 
    num=num+br; 
} 
void show_record() // FUNCTION - SHOW RECORD(S) 
{ 
    if (num==0) 
    { 
     cout<<"\t\t by far there are no pacient at all \n"; 

    } 
    cout<<"\n \t\t\t list with all pacients"<<num<<endl; 
    for(int i=0;i<num;i++) 
    { 
    cout<<"\n Name:"; 
    cout<<"\n >> "<<grupa[i].name; 
    cout<<"\n Surname:"; 
    cout<<"\n >> "<<grupa[i].surname; 
    cout<<"\n Lastname:"; 
    cout<<"\n >> "<<grupa[i].lastname; 
    cout<<"\n edinen:"; 
    cout<<"\n >> "<<grupa[i].edinen; 
    cout<<"\n Ill from:"; 
    cout<<"\n >> "<<grupa[i].illness; 
    cout<<"\n\n"; 

    } 
} 
void remove_record() // FUNCTION - DELETE RECORD(S) 
{ 
    int k,index; 
    for(int i=0;i<num;i++) 
    { 
    cout<<"\n("<<i+1<<") "<<endl; 
    cout<<"Pacient name: "<<grupa[i].name<<endl; 
    cout<<"Surname: "<<grupa[i].surname<<endl; 
    cout<<"Lastname: "<<grupa[i].lastname<<endl; 
    cout<<"edinen: "<<grupa[i].edinen<<endl; 
    cout<<"Ill from: "<<grupa[i].illness<<endl<<endl; 
    } 
    cout<<"Who do you want to delete? \n >> "; 
    cin>>index; 
    for(k=0;k<num;k++) 
    { 
     if(k>=index) 
     { 
      grupa[k].edinen=grupa[k+1].edinen; 
     } 
     if(k==num-1) 
      break; 
     else 
      cout<<"BY FAR THERE IS NO DATA HERE"<<endl; 
    } 
    num--; 

     for (i=0;i<num;i++) 
    { 
    cout<<"Pacient name: "<<grupa[i].name<<endl; 
    cout<<"Surname: "<<grupa[i].surname<<endl; 
    cout<<"Lastname: "<<grupa[i].lastname<<endl; 
    cout<<"edinen: "<<grupa[i].edinen<<endl; 
    cout<<"Ill from: "<<grupa[i].illness<<endl<<endl; 
    } 
} 
void print_count() // FUNCTION - PRINTING 
{ 
    cout<<"\n \t\t\t NUMBER OF PACIENTS - "<<num<<endl; 
    cout<<""<<endl; 
} 


void search_record() // FUNCTION - SEARCHING 
{ 
    int flag=0; 
    long int tempegn; 
    cout<<"\n Type the edinen for the pacient you search: "; 
    cout<<"\n >> "; 
    cin>>tempegn; 
    for (int i=0;i<n;i++) 
     if(tempegn==grupa[i].edinen) 
     { 
    cout<<"Pacient name: "<<grupa[i].name<<endl; 
    cout<<"Surname: "<<grupa[i].surname<<endl; 
    cout<<"Lastname: "<<grupa[i].lastname<<endl; 
    cout<<"edinen: "<<grupa[i].edinen<<endl; 
    cout<<"Ill from: "<<grupa[i].illness<<endl<<endl; 
      flag++; 
     } 
     if (!flag) 
     { 
      cout<<"\n\t PACIENT WITH THAT NAME DOESNT EXCIST \n\n"; 
     } 
} 

void main() // MAIN FUNCTION (MENU) 
{ 
    int choice; 
    do 
    { 
     cout<<"\n\t\t******************* Menu *******************"<<endl; 
     cout<<"\t\t*            *"; 
     cout<<"\n\t\t*   1.Add new pacient     *"; 
     cout<<"\n\t\t*   2.Search for a pacient by edinen *"; 
     cout<<"\n\t\t*   3.Delete pacient     *"; 
     cout<<"\n\t\t*   4.List with all pacients   *"; 
     cout<<"\n\t\t*   5.Exit        *"; 
     cout<<"\n\t\t*            *\n"; 
     cout<<"\t\t************************************************"<<endl; 
     cout<<"\n Type your choice! "; 
     cout<<"\n >> "; 
     cin>>choice; 
     switch(choice) 
     { 
     case 1:{print_count();add_record();break;} 
     case 2:{print_count();search_record();break;} 
     case 3:{print_count();remove_record();break;} 
     case 4:{print_count();show_record();} 
     } 
    } 
    while(choice!=5); 
} 
+0

我說我是程序員嗎? –

+0

你問的關於你使用'fstream'系列是諷刺的,因爲使用輸入的'iostream'庫/輸出大約是這個代碼不以任何方式引腳它需要C++的。 – WhozCraig

+0

在'你有一個可能的緩衝區溢出情況add_record'功能。什麼是唯一'NUM + br'大於'n'?那麼你會超出爲grupa分配的內容。 –

回答

0

我建議你讀一本書,或者至少有一些關於此文檔。此外,要了解這種投入的工作,它需要的OOP概念知識。至於我注意到你沒有這樣的知識,我建議你堅持一段時間,使用C的程序I/O函數,比如fprintf。谷歌搜索會顯示很多關於它們的資源,並且它們很容易理解,至少與那些來自C++的相比,正在使用

要回答你的問題,我會盡量解釋如何才達到你想要什麼。請注意,這是一個非常簡單的解釋,並非100%準確。

首先,需要打開該文件並創建一個I/O流中,使用用於ifstream輸入文件或ofstrsam輸出文件:

ifstream的F( 「input_filename」);

的ofstream克(「output_filename);

創建f和/或g之後(他們是不是都科目編號另外,f和g只是隨機的名字),你可以在你用同樣的方法使用它們cin和cout,在這種情況下,因爲f是輸入,所以它應該像cin一樣使用。

+0

這個嘛''後,您創建f和/或g(它們不在科目編號。另外,f和g只是隨機的名字),你可以在使用它們同樣的方式你使用cin和cout,在這種情況下,因爲f是用於輸入的,所以它應該像cin一樣使用。「我一直在搜索所有的時間。謝謝你PAUL92 謝謝你給我的所有答案!我認爲不知道如何做到這一點的問題實際上並不是在嘗試一切,而是在問一些愚蠢的問題!我的錯! –

+0

我很高興它有幫助。請考慮upvoting和/或接受我的答案。這是如何在stackexchange網絡上表示感謝。 – Paul92