简体   繁体   English

从输出函数创建文件

[英]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);我正在通过字符“a”调用下面的函数,我想通过字符“b”调用另一个函数以在 displayEntry(i) 之后创建一个文件; I am confused to create a file from display fun(), can anyone please help me out ?我很困惑从 display fun() 创建文件,有人可以帮我吗?

In my assignment it is written as below :在我的作业中,它是这样写的:

(a) Search a certain last name (b) Save the search result in a file (a) 搜索某个姓氏 (b) 将搜索结果保存在文件中

Where (a) is done but got stuck with (b)...........哪里(a)已经完成但被(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:使用 fstream 来读写文件:

#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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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