HELP How to process multiple files in C++

In summary, the conversation discusses a problem with processing multiple files in a C++ program. The person is struggling to increment the files and is seeking a quick and easy solution. The suggested solution involves using a std::stringstream to construct the file names and breaking the larger problem into smaller parts. Another suggestion is to use a shell script on Unix to run the program 500 times without having to deal with the specific file name format.
  • #1
aLostProgramm
2
0
HELP! How to process multiple files in C++

I have been bashing my head against my desk in my cubical trying to figure this out. I have approximately 500 files I would like to read in, one at a time, into a C++ program, do my processing, and send the results to a new file. I can do the processing just fine, but I can't increment the files. Below is what I have:

Code:
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
  ifstream infile;
  ofstream outfile;

  float temp1,temp2;

  infile.open ("bTemp[COLOR="Red"]5[/COLOR].txt");
  outfile.open ("finalBtemp[COLOR="red"]5[/COLOR].txt");

  while (!infile.eof())
    {
      infile >> temp1 >> temp2;
      temp1 = (temp1/100)+100;
      temp2 = (temp2/100)+100;
      outfile << temp1 << ' ' << temp2<<endl;
    }

  infile.close();
  outfile.close();

  return 0;

}
But instead of the highlighted 5's, I'd like it to be incremented, i.e. bTemp1.txt, bTemp2.txt, etc. Setting up the loop won't be a problem, I just need a quick, easy way to increment the files so I don't have to run the program once, change the program, run again... 500 times. I just want to send it through the loop.

Thanks in advance for a prompt response.
 
Technology news on Phys.org
  • #2


Use a std::stringstream to construct the names.
 
  • #3


How about:

Code:
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
  for (int i = 0; i < 500; ++i)
  {
    char infilename[256];
    char outfilename[256];
    sprintf(infilename, "bTemp%03d.txt", i);
    sprintf(outfilename, "finalBtemp%03d.txt", i);

    ifstream infile(infilename);
    ofstream outfile(outfilename);

    float temp1,temp2;

    while (infile >> temp1 >> temp2)
    {
      temp1 = (temp1/100)+100;
      temp2 = (temp2/100)+100;
      outfile << temp1 << ' ' << temp2<<endl;
    }
  }

  return 0;

}
 
  • #4


Secret programming technique -- breaking a larger problem into smaller parts!

You needed to stop thinking "how can I open these 500 files" and you needed to start thinking "how can I create 500 strings of a particular form?"

(but, for the record, when asking others for help, you should mention the full problem even if you are asking for help on one of these smaller problems)
 
  • #5


On unix, write a shell script that works through the file names (selected using wildcards) and runs your progam 500 times.

That way, you don't have to waste time writing code to do the special case of filenames in a particular format, when the Unix shells can already handle the general case.
 

1. How do I open and read multiple files in C++?

To open and read multiple files in C++, you can use the ifstream class. First, declare an ifstream object and pass in the file name as its argument. Then, use the open() method to open the file. You can use a loop to open and read multiple files sequentially.

2. How can I process data from multiple files at once?

To process data from multiple files at once in C++, you can use an array or vector to store the ifstream objects. Then, use a loop to read data from each file and process it accordingly.

3. Can I use file streams to write to multiple files in C++?

Yes, you can use the ofstream class to write to multiple files in C++. Just like with reading, you can use an array or vector to store the ofstream objects and use a loop to write data to each file.

4. How do I close multiple files in C++?

It is important to close files after reading or writing to them in C++. To close multiple files, you can use a loop and call the close() method on each ifstream or ofstream object.

5. Are there any libraries or functions that can help with processing multiple files in C++?

Yes, there are libraries and functions that can assist with processing multiple files in C++. For example, the dirent.h library has functions for listing and accessing files in a directory, which can be useful for processing multiple files. Additionally, you can create your own functions to handle file processing tasks and call them in a loop to process multiple files.

Similar threads

  • Programming and Computer Science
Replies
2
Views
659
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
32
Views
2K
Replies
52
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
7K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
27
Views
4K
Back
Top