Reputation: 537
I have the following class definition... and I'm wondering about the fstream objects.
#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
This is about the fifth different way I've tried to use fstream objects in the class, but none of them worked, got all different kinds of errors.
Here is an example of a function of how I'm implementing most recently
//**************************************************
// 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();
}
and this is an example of how I call it from 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);
}
But like I said, this is just one of my attempts, and it seems that no matter how I try to get the class to open the file and write to it, the program crashes or does something that is not the postcondition.
So, I'm wondering if it is even possible to use file streams inside classes. And if so, what do I need to keep in my in order to do it.
NOTE: I'm not necessarily looking for a specific implementation, I'm more curious about the "why" behind the implementation. I would like to know what I need to keep in mind so I can, in the future, be able to know what I'm doing.
Upvotes: 0
Views: 1370
Reputation: 563
It should be possible.
Perhaps there's something wrong with the fstream you're passing in. Double check 'dates'.
fstream inherits fail() from ios. It can be used to check whether your fstream is OK. You could use is_open() to check whether the file opened properly.
#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);
}
at least when it fails, you'll get a line number and can debug from there.
Also, I'm guessing you meant to change the second ios::out
ClientList::appendNode(...)
{
//dates.open("dates.txt", ios::out | ios::out);
dates.open("dates.txt", ios::in | ios::out | ios::app);
...
}
Upvotes: 1