简体   繁体   中英

Creating a file from a output function

I am calling the function below by a character 'a', I want to call another function by char 'b' to make a file after displayEntry(i); I am confused to create a file from display fun(), can anyone please help me out ?

In my assignment it is written as below :

(a) Search a certain last name (b) Save the search result in a file

Where (a) is done but got stuck with (b)...........

    // My code is :
void addBook::searchEntry() {
    char lastname[32];
    cout << "Enter last name : ";
    cin >> lastname;
    for(int i = 0;i < count;++i) {
        if(strcmp(lastname, entries[i].lastName) == 0) {
            cout << "Found ";
            displayEntry(i);
        }
        cout<<endl;
    }
}

use fstream to write and read from/to files:

#include <fstream> // add this to your inclusion 

// My code is :
void addBook::searchEntry()
{
    ofstream out("example.txt", ios::out);

    char lastname[32];
    cout << "Enter last name : ";
    cin >> lastname;
    for(int i = 0; i < count; ++i)
    {
        if(strcmp(lastname, entries[i].lastName) == 0)
        {
            cout << "Found ";
            displayEntry(i);
            out << lastname << endl;
        }
        cout<<endl;
    }

    out.close(); // don't forget to close files after you're done with using them 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM