Opening Files

Hey guys, Im not sure what is wrong with this function. It is supposed to be opening the file, and displaying the list of colors in the file. I have commented in a few things where I think the problem might be happening. Any help would be appreciated. Also this is my first time working with a void function, is a return value needed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 void getColorFamily(string colorList[], string familyName, string colorIndexFileName) {
	// This function opens a color family file and reads its colors.
	// Use an index file of colors to get the file name.
	// Open the file and read the colors into the colorList array

	string fileName = getColorFileName(familyName, colorIndexFileName);
	fileName = baseDir + fileName;
	fstream infile;
	cout << fileName << endl;      // I have checked, the right file name is being pulled
	infile.open(fileName);
	int i = 0;
	while (!infile.eof()) {
		infile >> colorList[i];     // This is where the orignal colorList is being replaced by the list in the file. (I think this is where problem is)
		i++;
		cout << colorList[i] << endl;
		if (i >= 10)
			break;
	}

	infile.close();

}
At line 14 you increment i before printing the value of colorList[i]. Maybe swap lines 14 and 15?

As a matter of style, I'd do the counted loop as a for loop, and check infile inside it:
1
2
3
4
5
6
for (int i=0; i<10; ++i) {
    if (!(infile>>colorList[i])) {
        break;
    }
    cout << colorList[i] << endl;
}

I think this is clearer code. It's also more correct because eof() won't be true until you try to read at the end of the file. So the existing code is likely to fail at line 13, and THEN set eof.
Functions with no type. The use of void:
http://www.cplusplus.com/doc/tutorial/functions/
Where do you check to see if the file was opened or not ?
Thank you! swapping lines 14 & 15 worked! I do agree that the code looks nicer, but im not sure I understand line 2. If (not(infile>>colorlist[i])). This says that as long as there is more to the file this statement will be false?

@SamuelAdams: Im not sure how i would be able to do that. On line 9 i checked to make sure the right file name was coming up, if that is what you mean. Otherwise im curious to see how to check if the file is being opened.
Let's look at if (not (infile >> colorlist[i])). You are right that there's a lot going on there. First the program executes infile >> colorlist[i]. If it turns out that we're at the end of the file, it will set the eof bit on infile. If it fails for some other reason, it will set the fail bit or bad bit etc.

infile >> colorlist[i] returns a reference to infile, so not(infile >> colorlist[i]) is the same as not infile, executed after the attempt to extract colorlist[i]. The not operation requires a bool, and the stream class defines a bool conversion that returns true if the stream is good, and false otherwise. So if you hit eof, bool(infile) is false and not(bool(infile)) is true.

The botton line is that if (stream >> variable) will return true if the extract succeeds and false otherwise
Thank You! This was very helpful
Topic archived. No new replies allowed.