Using ifstream to open multiple files

In summary: Band = BLACK;tempBand = BLUE;tempBand = RED;tempBand = GREEN;tempBand = YELLOW;}/* This function will calculate an individuals weight and provide an appropriate resistance band. */void weight_to_bands(int weight, bandRecord myList[]){ // Use the weight and band color to determine the resistance // of the band // // int resistance = totalResistance(myList); //
  • #1
ActionPotential
60
4
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 doesn't 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:

Code:
/* Trevor Novak - April 9, 2014 - "Bodylastics"
     This program will construct an array of structs to determine
     the resistance of corresponding resistance bands. It will also
     calculate an individuals weight and provide an appropriate band. */
       
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

const string SENTINEL = "stop";
enum bandType {YELLOW, GREEN, RED, BLUE, BLACK};

struct bandRecord
{
       string bandcolor;
       int weight;
       bool is_used;
};

/* Function Prototypes */ 

void setup(ifstream&, bandRecord[]);
void getBands(ifstream&, bandRecord[]);
bandType str_to_color(const string&);
void printBands(ofstream&, const bandRecord[]);
int totalResistance(const bandRecord[]);
void resetBands(bandRecord[]);
void weight_to_bands(int, bandRecord[]);

int main()
{
    bandRecord myList[BLACK+1];
    ifstream inData;
    ofstream outData;
    string bandcolor;

  inData.open("setupData.txt"); // Open setupData File
  if (not inData){
     cout << "Can't open data.txt" << endl;
     system ("pause");
     return 1;}

  outData.open("outData.txt"); // Generate outData File
  if (not outData){
     cout << "Can't open output.txt" << endl;
     system ("pause");
     return 1;}

    setup(inData, myList); // Calls the setup procedure.
    
    /* The follow lines are tests to find the code errors */
    cout << myList[1].bandcolor << " " << myList[1].weight << endl;
    cout << myList[4].bandcolor << " " << myList[4].weight << endl;
    inData.close(); 
    

   [b]/* -- THIS IS THE PROBLEM BELOW - I NEED DATA
      FROM THIS FILE AND IT IS NOT OPENING */

  inData.open("bandData.txt"); // Open bandData File
  if (not inData){
     cout << "Can't open data.txt" << endl;
     system ("pause");
     return 1;}[/b]
   
    getBands(inData, myList); // Call getBands procedure. 

    /* The follow lines are tests to find the code errors */
    cout << myList[2].is_used << " " << myList[2].bandcolor << endl;
    cout << myList[1].is_used << " " << myList[1].bandcolor << endl;
    cout << myList[4].is_used << " " << myList[4].bandcolor << endl;
   
    printBands(outData, myList); // Call printBands procedure.
	
system("pause");
return 0;

}

/* Function/Procedure Definitions */

/* This procedure will read in data from a text file 
   into a struct called bandRecord and constructs an 
   array of type bandRecord. The array is indexed
   by the enum type, bandType. */ 

void setup(ifstream& inData, bandRecord myList[])
{     
	for(int i=YELLOW; i <= BLACK; i++)
	{
      inData >> myList[i].bandcolor >> myList[i].weight;
      myList[i].is_used = false;
    }   
}

/* This procedure will read in band colors as tempString.
   It will then call by value str_to_color(tempString)
   to assign its (enum type) value to a bandType tempBand.
   
   It will then set the .is_used flag to true according
   to index of the temporary bandType. */

void getBands(ifstream& inData, bandRecord myList[])
{
 string tempString;
 bandType tempBand;

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

/* This function will create a temporary character array
   based on the contents of tempString. The switch will then
   determine the first letter of the string and assign tempBand
   the value of the corresponding enum-type. To resolve the issue
   of black and blue, upon encountering 'b' it will test the third
   letter to determine if it is "blAck" or "blUe". */

bandType str_to_color(const string& tempString)
{
 bandType tempBand;

      switch(tempString[0])
      {
       case 'y': tempBand = YELLOW;
                 break;
       case 'g': tempBand = GREEN;
                 break;
       case 'r': tempBand = RED;
                 break;
       case 'b': 
                 if(tempString[2] == 'u')
                 tempBand = BLUE;
                 if(tempString[2] == 'a')
                 tempBand = BLACK;
                 break;

      return tempBand;
      }
}

void printBands(ofstream& outData, const bandRecord myList[])
{
      
   for(int i=YELLOW; i <= BLACK; i++)
   {
      if(myList[i].is_used == true)
        {
         outData << myList[i].bandcolor << myList[i].weight << endl;
        }
   }

}
 
int totalResistance(const bandRecord[])
{
    
    
}
void resetBands(bandRecord[])
{
     
     
}
void weight_to_bands(int, bandRecord[])
{
     
     
}

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

Regards,
ActionPotential
 
Last edited:
Technology news on Phys.org
  • #2
If you call std::if stream::eek:pen on an already connected stream, the open call will fail and the stream will be marked as failed.

You need to close the stream before you (re)open it. It's also a good idea to call clear between calling close and open.
 
  • #3
Hey DH! Been reading your posts for the last 8 years ;) In the posted code I have a close after the initial file open (It's a mess to read right now), however, when I had used inData.clear(); it was causing it to crash.

I just changed the heading of the getBands procedure from
Code:
 getBands(ifstream&, bandRecord[]) to getBands[istream&, bandRecord[])
so that I could use
Code:
 cin
to test an input.

I have now discovered an issue with getBands procedure when it calls
Code:
 str_to_band(tempString);
. As an example, I typed in "red" and it crashed. Tried "blue" and it crashed. So, I appear to have an issue with that procedure and perhaps that is why the data wasn't read in from the second file.

Going to continue troubleshooting. I am sure it is something silly.

Thank you for your advice, I appreciate it.
 
  • #4
HAHA just found it!

I was returning the value inside the switch statement but wasn't returning a value of bandType at before the end of the function. So, I wasn't passing anything in the call by value.

Coding is FUNSTRATING!
 
  • #5
ActionPotential said:
I just changed the heading of the getBands procedure from
Code:
 getBands(ifstream&, bandRecord[]) to getBands[istream&, bandRecord[])
so that I could use
Code:
 cin
to test an input.
No doubt just a copy/paste typo, but in what you changed to, the first bracket right after getBands should be a parenthesis.
Code:
getBands[B]([/B]istream&, bandRecord[])
 

1. How do I open multiple files using ifstream?

To open multiple files using ifstream, you can use a loop to iterate through each file and open them one by one. You will need to create an ifstream object for each file and use the .open() function to specify the file path.

2. Can I open all files in a directory using ifstream?

Yes, you can use the std::filesystem library to iterate through all files in a directory and open them using ifstream. You will need to use the std::filesystem::directory_iterator function to get a list of files in the directory.

3. How do I check if a file was successfully opened using ifstream?

You can use the is_open() function to check if the file was successfully opened. This function returns a boolean value of true if the file was opened successfully and false if there was an error.

4. Is there a limit to the number of files I can open using ifstream?

There is no limit to the number of files you can open using ifstream. However, keep in mind that opening a large number of files can use up a lot of memory and can slow down your program.

5. How do I close all files opened using ifstream?

You can use the close() function to close each file that was opened using ifstream. It is important to close all files to avoid any memory leaks or errors.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
925
Replies
10
Views
961
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
5
Views
885
  • Programming and Computer Science
Replies
5
Views
820
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top