【问题标题】:can fstream objects be used inside a class?fstream 对象可以在类中使用吗?
【发布时间】:2013-01-28 22:51:47
【问题描述】:

我有以下类定义...我想知道 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);        
    }

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

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

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

【问题讨论】:

  • 这是可能的并且完全正常。它是如何失败的?
  • “实施”何时成为“使用”的同义词?
  • 您确定问题出在fstream 上吗? numInterests == interests.size()?单步执行代码以查看它在哪一行崩溃。你还在任何地方初始化ListNode *head;吗?
  • @MichaelPhoenix:该代码是std::vector 的一部分。问题在于 newNode-&gt;interests[index] 您正在访问越界元素(您只能访问从 0interests.size() - 1 的元素)。
  • int index = 0; 更改为 std::size_t index = 0;size() 返回一个无符号整数 (std::size_t),您正试图将其与有符号整数进行比较。但是,您应该考虑使用迭代器而不是索引。

标签: c++ class fstream


【解决方案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);
    ...
}

【讨论】:

  • 是的,实际上没有任何东西进来,所以它只会出去......我会尝试断言,看看它会返回什么
  • 大声笑......我已经出去了 |在我访问文件的所有地方,duh!感谢您的捕获...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 2017-01-30
  • 2019-04-27
  • 2010-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多