I have a program which i need to run with 2 input files 1 is no problem but i have no idea where to start with having it run with 2 anyone have any ideas?

basically heres how it goes
1)have the program run with data01.dat
2)outputs results to output.dat
3)loops back up to the top of function
4)runs with data02.dat
5)outputs again to output.dat

3-5 is what i dont know how todo

thanks in advance for any help

One way to do it is to put all the code to read and write a file in a function, then call that function twice from main() with the name of the input file eacj time, something like below except you should use a different name for the function.

void foo(const char* inputfilename, const char* outputfilename)
{

}

int main()
{
   foo("data01.dat", "out1.dat");
   foo("data02.dat", "out2.dat");
}

can i output to the same file twice without having it overwrite using my outFile stream?

can i output to the same file twice without having it overwrite using my outFile stream?

Yes. It depends on how you open the output file. You need to append it instead of truncate it. ofstream output(filename,[b]ios::app[/b]); //The bolded sets to append instead of truncate.

I don't know if you're going to end up doing this, but if you want to loop and read in a number of files, with systematic names, like:

data1.dat
data2.dat
data3.dat
data4.dat
...
data9.dat

you can use a std::stringstream to generate the names from the loop variable, like this:

/* define the "constant" parts of the filename */
std::string rootName("data");
std::string extension(".dat");

/* now loop through the files, constructing the full filename as you go */
for(unsigned i = 1; i <= 9; ++i){
    std::stringstream ss(rootName);
    ss << i << extension;  /* ss is set to "datai.dat", where i = 1, .., 9 */

    /* Open the file, need to use the "str()" method of the string stream to convert    */
    /* the string stream into a std::string, then use the "c_str()" method on the       */
    /* resulting string to convert this to a C-style char array, which will be accepted */
    /* by std::ifstream::open().                                                        */
    infile.open(ss.str().c_str(), std::ios::in);
    
    /* Do things with file here ... */
}

Yes. It depends on how you open the output file. You need to append it instead of truncate it. ofstream output(filename,[b]ios::app[/b]); //The bolded sets to append instead of truncate.

thankyou i will definitely be using that

I don't know if you're going to end up doing this, but if you want to loop and read in a number of files, with systematic names, like:

data1.dat
data2.dat
data3.dat
data4.dat
...
data9.dat

you can use a std::stringstream to generate the names from the loop variable, like this:

/* define the "constant" parts of the filename */
std::string rootName("data");
std::string extension(".dat");

/* now loop through the files, constructing the full filename as you go */
for(unsigned i = 1; i <= 9; ++i){
    std::stringstream ss(rootName);
    ss << i << extension;  /* ss is set to "datai.dat", where i = 1, .., 9 */

    /* Open the file, need to use the "str()" method of the string stream to convert    */
    /* the string stream into a std::string, then use the "c_str()" method on the       */
    /* resulting string to convert this to a C-style char array, which will be accepted */
    /* by std::ifstream::open().                                                        */
    infile.open(ss.str().c_str(), std::ios::in);
    
    /* Do things with file here ... */
}

thanks i think thats what i need to use for my input files i will just have to fool around with that some

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.