可以在一个类中使用fstream对象吗?

时间:2013-01-28 22:51:47

标签: c++ class fstream

我有以下类定义......我想知道fstream对象。

#ifndef CLIENTLIST_H
#define CLIENTLIST_H
#include <string>
#include <vector>
#include <fstream>
using namespace std;

class ClientList
{
private:
// A structure for the list
struct ListNode
    {
        char gender;
        string name;
        string phone;
        int numInterests; // The number of interests for the client
        vector<string> interests; // list of interests
        string match;
        struct ListNode *next;  // To point to the next node
    }; 

    ListNode *head;            // List head pointer
    string name;

    void detach(string); // to unmatch the corresponding client

public:
    // Constructor
    ClientList();

    // Destructor
    ~ClientList();

    // Linked list operations
    void appendNode(char, string, string, int, vector<string>, string, fstream &);
    string interestCompare(vector<string>, string, fstream &);
    void unmatch(string, ClientList, fstream &);
    void printMatch(fstream &);
    void printFree(fstream &);

};
#endif

这是我尝试在课堂上使用fstream对象的第五种不同方式,但它们都没有工作,得到了各种不同的错误。

以下是我最近实施的功能的一个示例

//**************************************************
// appendNode appends a node containing the        *
// value pased into num, to the end of the list.   *
//**************************************************
void ClientList::appendNode(char gen, string nm, string ph, int numIntr, 
                            vector<string> intr, string mch, fstream &dates)
{
    dates.open("dates.txt", ios::out | ios::out);
    ListNode *newNode;  // To point to a new node
    ListNode *nodePtr;  // To move through the list

    // Allocate a new node and store data in it.
    newNode = new ListNode;
    newNode->gender = gen;
    newNode->name = nm;
    newNode->phone = ph;
    newNode->numInterests = numIntr;
    newNode->interests = intr;
    newNode->match = mch;
    newNode->next = NULL;

    // If there are no nodes in the list
    // make newNode the first node.
    if (!head)
        head = newNode;
    // Otherwise, insert newNode at end.
    else  
    {
        // Initialize nodePtr to head of list.
        nodePtr = head;

        // Find the last node in the list.
        while (nodePtr->next)
            nodePtr = nodePtr->next;

        // Insert newNode as the last node.
        nodePtr->next = newNode;
    }

    dates << "\nClient: " << newNode->gender << ", " << newNode->name << ", "
          << newNode->phone << ", " << newNode->numInterests << ", ";
    for (int index = 0; index < newNode->numInterests; index++)
        dates << newNode->interests[index] << ", ";
    dates << newNode->match << ".\n";



    cout << "\n\nAPPENDED\n\n";
    dates.close();
}

这是我如何从main()

调用它的一个例子
//append to file
    if (gender == tolower('m'))
    {       

        match = Female.interestCompare(interests, name, dates); // compare the vector of interests to the client interests
        // in the female list

        Male.appendNode(gender, name, phone, numInterests, interests, match, dates);        
    }

但就像我说的那样,这只是我的一次尝试,似乎无论我如何尝试让类打开文件并写入它,程序崩溃或做一些不是后置条件的事情。

所以,我想知道是否甚至可以在类中使用文件流。如果是这样的话,为了做到这一点,我需要保留什么。

注意:我不一定要寻找具体的实现,我对实施背后的“原因”更加好奇。我想知道我需要记住什么,以便将来能够知道我在做什么。

1 个答案:

答案 0 :(得分:1)

应该可以。

也许你传入的fstream有问题。请仔细检查'日期'。

fstream从ios继承fail()。它可用于检查您的fstream是否正常。您可以使用is_open()来检查文件是否正确打开。

#include <assert.h>

// stuff...
fstream dates("dates.txt", fstream::in | fstream::out);
if (dates.is_open())
{
    cout << "dates.txt opened ok" << endl;
}
dates.close();

if (gender == tolower('m))
{
    assert(dates.fail() == false);

    match = Female.interestCompare(interests, name, dates);

    assert(dates.fail() == false);

    Male.appendNode(gender, name, phone, numInterests, interests, match, dates);        
}

至少在它失败时,你会得到一个行号并可以从那里进行调试。

另外,我猜你想改变第二个ios :: out

ClientList::appendNode(...)
{
    //dates.open("dates.txt", ios::out | ios::out);
    dates.open("dates.txt", ios::in | ios::out | ios::app);
    ...
}
相关问题