Пример #1
0
//option 3
void TransferTextToBinary
    (
    fstream& ioFile,
    bool& successful
    )
{
    string fileName;
    ReadString("Enter name of input textfile containing club "
        "members: ",fileName);
    ifstream inputTextFile(fileName);
    ioFile.open("working.bin",ios::binary| ios::out| ios::in| ios::trunc);
    if(!inputTextFile)
    {
        cout << "\nError opening input textfile.\nReturning to menu." << endl;
        Pause();
        successful = false;
    }
    else if(!ioFile)
    {
        cout << "\nError opening working binary file."
            "\nReturning to menu." << endl;
        Pause();
        successful = false;
    }
    else
    {
        string temp;
        int count = 1;
        //get the name
        while(getline(inputTextFile,temp,'\n'))
        {   
            if(count == 1)
            {
                //clear the char array
                memset(member.name,'\0',31);
                for(unsigned int i = 0 ; i < temp.length(); i++)
                    member.name[i] = temp[i];
                count++;
            }
            //get the age and balance
            getline(inputTextFile,temp,'\n');
            if(count == 2)
            {
                string::size_type space = temp.find(' ');
                string tempAge = temp.substr(0,space);
                //find the age and store it in struc
                member.age = atoi(tempAge.c_str());
                string::size_type dollar = temp.find('$');
                string tempBalance = temp.substr(dollar+1);
                //find the balance and store it in struc
                member.balance = atof(tempBalance.c_str());
                count--;
            }
            //write the struc into the binary file
            ioFile.write((char *)&member, sizeof(ClubMember));
        }
        successful = true;
        inputTextFile.close();
    }
}
Пример #2
0
//option 9
void DisplayAnyTextfile()
{
    string fileName;
    ReadString("Enter name of file to display: ", fileName);
    DisplayTextfile(fileName);
}