File IO function call problem

Hi everyone.
I'm having trouble with 2 functions in my program; one for reading in data from a file and one for writing data to a file.

Both of the function calls in the main function cause it not to compile and give the error "C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'", and a bunch of other stuff I don't understand.

The code in the writeToFile function works when I paste it into the main function, but I need to keep it as a separate function.

Something must be wrong with the code in readFromFile because it doesn't read in the data in the file either way, even though the data file is in the same directory as the .cpp.

Thanks in advance for your help! I know it's long, but here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<iostream>
#include<cstring>
#include<fstream>
#include<iomanip>

using namespace std;

const int MAX_CHAR = 101;
const int CAPACITY = 50;

struct Item
{
	char courseName[MAX_CHAR];
	char task[MAX_CHAR];
	char dueDate[MAX_CHAR];
};

char readInOption(char prompt[]);
void getItem(Item list[], char prompt1[], char prompt2[], char prompt3[], int maxChar, int& index);
void displayAll(Item list[], int index);
void readFromFile(ifstream in, Item list[], int maxChar, int& index);
void writeToFile(ofstream out, const Item list[], int index);

int main ()
{
	char option;
	int index = 0;
	Item list[CAPACITY];
	ifstream inFile;
	ofstream outFile;

	readFromFile(inFile, list, MAX_CHAR, index);
	
	cout << "Welcome to the assignment database.\n";

	do
	{
		cout << "a. Add a task.\n"
			 << "d. Display all tasks.\n"
			 << "f. Find a task by course.\n"
			 << "q. Quit.\n";
		
		option = readInOption("Please enter an option: (a, d, f, q) ");

		switch (option)
		{
		case 'a':
			cout << endl;
			getItem(list, "Please enter the course name: ", "Please enter the task name: ", "Please enter the due date: ", MAX_CHAR, index);
			break;
		case 'd':
			cout << endl;
			displayAll(list, index);
			break;
		case 'f':
			cout << "f is invoked\n";
			break;
		}
	}
	while (option != 'q');

	writeToFile(outFile, list, index);

	return 0;
}

char readInOption(char prompt[])
{
	char option;

	cout << prompt;
	cin >> option;
	tolower(option);

	while (option != 'a' && option != 'd' && option != 'f' && option != 'q')
	{
		cin.ignore(100, '\n');
		cout << "Invalid entry.\n"
			 << prompt;
		cin >> option;
	}
	cin.ignore(100, '\n');
	return option;
}

void getItem(Item list[], char prompt1[], char prompt2[], char prompt3[], int maxChar, int& index)
{
	char confirm;
	
	cout << prompt1;
	cin.get(list[index].courseName, maxChar, '\n');
	cin.ignore(100, '\n');
	cout << prompt2;
	cin.get(list[index].task, maxChar, '\n');
	cin.ignore(100, '\n');
	cout << prompt3;
	cin.get(list[index].dueDate, maxChar, '\n');
	cin.ignore(100, '\n');

	cout << "You entered " << list[index].courseName << ", "
		<< list[index].task << ", "
		<< list[index].dueDate << endl
		<< "Is this correct? (y/n)\n";
	cin >> confirm;
	
	tolower(confirm);
	while (confirm != 'y' && confirm != 'n')
	{
		cin.ignore(100, '\n');
		cout << "Invalid entry. Type y or n.\n";
		cin >> confirm;
		cin.ignore(100, '\n');
	}
	if (confirm == 'y')
	{
		cout << "Item confirmed.\n";
		index++;
	}
	else if (confirm == 'n')
		cout << "Item discarded.\n";
	cin.ignore(100, '\n');
}

void displayAll(Item list[], int index)
{
	int noOfRecords;

	cout << setw(20) << left << "Class"
		 << setw(30) << left << "Task"
		 << setw(20) << left << "Due date" << endl;

	for (noOfRecords = 0; noOfRecords < index; noOfRecords++)
	{
		cout << setw(20) << left << list[noOfRecords].courseName
			 << setw(30) << left << list[noOfRecords].task
			 << setw(20) << left << list[noOfRecords].dueDate << endl;
	}
	cout << endl;
}

void readFromFile(ifstream in, Item list[], int maxChar, int& index)
{
	in.open("tasks.txt");
	while (!in.eof())
	{
		in.getline(list[index].courseName, maxChar, ';');
		in.getline(list[index].task, maxChar, ';');
		in.getline(list[index].dueDate, maxChar, ';');

		index++;
	}
}

void writeToFile(ofstream out, const Item list[], int index)
{
	int noOfRecords;

	out.open("tasks.txt");
	for (noOfRecords = 0; noOfRecords < index; noOfRecords++)
	{
		out << list[noOfRecords].courseName << ";"
			<< list[noOfRecords].task << ";"
			<< list[noOfRecords].dueDate << '\n';
	}
}
Filestream objects must be passed by reference. This allows them to be modified by the function and also allows them to maintain the same file information between the entire program. I don't see why you need them in main since you're not doing anything with them there. You should just move them to your function anyways.
That took care of it. Thank you!

But now it's putting blank lines between the entries already in the data file and the new ones I make when I run the program, and again when it writes to the data file before quitting.

Any guesses why that might be?

Thanks again for your help.
I actually couldn't get it to run on my system, it kept crashing. I have no idea why, but I didn't spend the extra time to debug it. Use www.pastebin.com and show what the file looks like before reading/writing, then after and I'll figure it out.
The file only has a few entries, and it's only leaving the spaces between the data input from the file and entries added while the program is running. I've looked over all my \n and endl and I can't figure out why it's doing that.

Before adding entries in the program: http://pastebin.com/G4v5WdKj
After: http://pastebin.com/74p254ZK

I don't know if it matters, but after the program writes the file, the cursor is always at the beginning of the new line.

Thanks again for helping me out. :)
Look over your file very carefully. Remove any extra whitespace, specifically, the ony at the very end of the file.

You're going to have to mess around with things, But I believe what's happening is that you keep ending up with one extra line at the end of your files. The reason is because of this:
1
2
3
4
5
6
for (noOfRecords = 0; noOfRecords < index; noOfRecords++)
	{
		out << list[noOfRecords].courseName << ";"
			<< list[noOfRecords].task << ";"
			<< list[noOfRecords].dueDate << '\n';
	}


The '\n' will put a new line after every write to the file. This isn't what you want. You only want it written everytime it's not the index of the array.
Topic archived. No new replies allowed.