Using ifstream to open multiple files

I am currently working on a C++ program for school. I am actually not finding too much difficulty in constructing the functions, enum-types, arrays and structs, however, I am finding great difficulty in using on ifstream variable to open multiple files.

I have posted the entire code that I have so far (even though I have pinpointed the issue to not properly opening the second file in ifstream).

I spent a couple of hours getting rid of certain functions/procedures, loops and variables and I get the same output (if what I removed doesnt crash it). I also get the same output whether I "open" the second file or not (meaning I removed all of the code for it and got the same output).

Here is the code (it's not finished because I am stuck on this file issue but hopefully my comments give you a clear enough picture to help). It's a bit messy since I am now in debug mode versus program mode:


I'll continue working on it and thank you for your time and assistance.

Regards,
ActionPotential
Last edited on
It's possible that the stream inData was left in some unusable state, depending upon what took place previously. Before the open() at line 62, you could clear the flags:
inData.clear(); // reset flags for stream

Or just use two completely separate ifstreams for the two inputs.
Last edited on
inData.clear(); was causing it to crash for some odd reason.

However, I changed the other file to istream& inData so that I could also use cin to test input and now I am getting an issue with my getBands loop (so perhaps that has been the issue all along).

Going to continue troubleshooting. I never usually have problems with the programs but this one is annoying (I would use classes and do this quickly but we are supposed to use all the different things we have learned).
A couple of problems that I see:

1) In setup, you're indexing myList from 0 - 5 (total of 6). If you have only 5 records in the file, you're going to hit eof and set the failbit on the file. You have no check that the read succeeded.

2) Line 62, you're try to open a file using the same ifstream instance where the failbit was previously set and never cleared.

You have a couple of options:
1) At line 62, make sure the failbit from any previous operation is cleared:
1
2
 
  inData.clear();


2) Assign a different ifstream instance:
1
2
3
4
 
  ifstream bandfile ("banddata.txt"); 
  if (! bandfile) 
...

Incidentally, I resolved the file issues and realized what the ultimate problem was all along! I was returning the bandType in the switch (in getBands) but I was never returning the bandType to the function.

So when I assigned tempBand = str_to_bands(tempString) I didn't have anything to call by value since I wasn't passing anything.

I'm sure I will hit another snag.
1) In setup, you're indexing myList from 0 - 5 (total of 6). If you have only 5 records in the file, you're going to hit eof and set the failbit on the file. You have no check that the read succeeded.


Are you referring to bandRecord myList[BLACK+1]?

If so, it seems correct to me. YELLOW == 0, BLACK == 4 ; BLACK+1==4+1 == 5 (we have 5 elements). Am I thinking incorrectly?
Is this still your current code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getBands(ifstream& inData, bandRecord myList[])
{
 string tempString;
 bandType tempBand;

 inData >> tempString; 
 
	while(inData)
	{ 
		tempBand = str_to_color(tempString);
		myList[tempBand].is_used = true;
    }
 
}

That looks like an infinite loop if the first read was successful.

tempBand is not initialised so could contain garbage. (Sorry - I missed that it is modified by the value from str_to_color() ) There's no way to either read another item from the file, or move on to the next array element.
Last edited on
Are you referring to bandRecord myList[BLACK+1]?

No. That is fine. You're allocating 5 elements are expected.

I was referring to the for loop at line 92 and the possibility that you could be setting the eofbit which would cause the subsequent open of the second file (using the same ifstream instance) to fail because the error bits has not been cleared.
Chervil and Abstraction - you are both correct. I changed that code after posting (when I realized my issue wasnt the file stream but a specific function).

That was indeed a problematic loop (it was running an additional time and outputting wrong data -- I also didn't have an update of control at the end of the loop) and I resolved it by using a SENTINEL controlled loop instead of the EOF loop. When it reads in "stop" from the data it will exit the loop.

Thank you both for your help so far! You gave me lots of things to think about and helped me troubleshoot the issues. Now, I will be implementing the last part of the program and I am sure I will be back with more questions, haha.

Coding is FUNSTRATING!!!
Last edited on
Topic archived. No new replies allowed.