C ++将记录保存到文件中

时间:2013-12-18 08:21:41

标签: c++

我一直在想如何整夜“保存到档案”这件事,但似乎运气不在我身边...... 我是新手,我想学习,就是这样。 我来自保加利亚,对于那些想知道什么是“edinen”的人来说,这是你作为该国家成员的公民号码。 (我仍然不知道如何解释这个...)

这就是我所拥有的 (我已经包含了“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);
}

1 个答案:

答案 0 :(得分:0)

我建议你读一本书,或者至少有一些关于这方面的文件。此外,要了解这种输入是如何工作的,需要了解oop概念。据我所知,你没有这方面的知识,我建议你坚持使用C语言的程序i / o函数,比如fprintf。谷歌搜索将显示许多关于它们的资源,它们很容易理解,至少与C ++相比,就像你正在使用的那些。

要回答你的问题,我会尝试解释如何实现你想要的。请注意,这是一个非常简化的解释,并不是100%准确。

首先,您需要打开文件并创建一个i / o流,使用ifstream作为输入文件,或ofstrsam作为输出文件:

ifstream f(“input_filename”);

ofstream g(“output_filename);

创建f和/或g之后(它们都不是必需的。另外,f和g只是随机名称)你可以像使用cin和cout一样使用它们,在这种情况下,因为f是for输入,它应该像cin一样使用。