HELP How to process multiple files in C++

Click For Summary

Discussion Overview

The discussion revolves around how to process multiple files in C++ efficiently, specifically focusing on incrementing file names programmatically to handle a large number of input and output files. The context includes coding techniques and potential alternative solutions outside of C++.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • The original poster describes their challenge with incrementing file names in a C++ program to process approximately 500 files.
  • One participant suggests using a std::stringstream to construct the file names dynamically.
  • Another participant provides a code snippet that utilizes a loop and sprintf to format the file names with zero-padding.
  • A later reply emphasizes the importance of breaking down the problem into smaller parts, suggesting a shift in perspective on how to approach the file handling.
  • Another suggestion involves using a shell script on Unix to handle the file processing, indicating that the shell can manage file names with wildcards effectively.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, including C++ solutions and Unix shell scripting. There is no consensus on a single best method, as different solutions cater to varying preferences and environments.

Contextual Notes

Some participants note the importance of understanding the full problem context when seeking help, indicating that incomplete information may hinder effective assistance.

Who May Find This Useful

This discussion may be useful for programmers looking to automate file processing in C++, as well as those interested in alternative methods using shell scripting for similar tasks.

aLostProgramm
Messages
2
Reaction score
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 ("bTemp5.txt");
  outfile.open ("finalBtemp5.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


Use a std::stringstream to construct the names.
 


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;

}
 


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)
 


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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
81
Views
8K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
52
Views
5K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 27 ·
Replies
27
Views
6K
  • · Replies 32 ·
2
Replies
32
Views
4K