C/C++ HELP How to process multiple files in C++

AI Thread Summary
To process multiple files in C++, a user sought help on how to increment file names for reading and writing. The initial code successfully processed a single file, but the user needed a way to automate the reading of 500 files named sequentially (bTemp1.txt to bTemp500.txt) and output the results to corresponding files (finalBtemp1.txt to finalBtemp500.txt). A solution was proposed using a loop and `sprintf` to format the file names dynamically. The revised code iterates through the desired range, constructs the input and output file names, and processes each file in turn. Additionally, a suggestion was made to consider using a shell script on Unix systems to manage file processing more efficiently, leveraging wildcards to handle file names without extensive coding. The discussion emphasized breaking down the problem into manageable parts and considering existing tools for automation.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top