   Reading and writing to files
  Im trying to write data from several files into another single file, but only the first file is being written. Is there anything Im doing wrong?outFile.open(outfile,std::ios::out|std::ios::binary|std::ios::app);for(int i=0; i<3; i++){    inFile.open(infile,std::ios::in | std::ios::binary);    while(!inFile.eof())    {   inFile.read(&ch,sizeof(ch));   outFile.write(&ch,sizeof(ch));    }    inFile.close();}outFile.close();
  Perhaps the infile's end of file flag wasn't reset when you opened a new file? Try this:outFile.open(outfile,std::ios::out|std::ios::binary|std::ios::app);for(int i=0; i<3; i++){    fstream inFile(infile,std::ios::in | std::ios::binary);    while(!inFile.eof())    {   inFile.read(&ch,sizeof(ch));   outFile.write(&ch,sizeof(ch));    }}outFile.close();Also, consider renaming some of the variables. inFile and infile? I'm not sure how common this is, but I like to make my array/vector variables plural so maybe inFileNames would be better than infile. Just a suggestion! Hope my code changes work.   ;Quote:Original post by TheBinaryManIm trying to write data from several files into another single file, but only the first file is being written. Is there anything Im doing wrong?*** Source Snippet Removed ***I was working on something similar not too long ago, and found that I had to call inFile.clear(); because reaching EOF would cause some error bit flags to be set, and if you don't clear them than it won't open the next file. Hope that helps a little.   ;  Thank you Chadwell and nobodynews, both of your solutions worked.Quote:Original post by nobodynewsAlso, consider renaming some of the variables. inFile and infile? I'm not sure how common this is, but I like to make my array/vector variables plural so maybe inFileNames would be better than infile. Just a suggestion! Hope my code changes work.I know, I do name my variables with better care, I was just throwing this code together to get it working.   ;  As a rule of thumb, don't reuse file streams. It's needlessly complicated. The library is designed to make it easy to use a separate file stream for each file. In a loop, you do that by declaring the file stream inside the loop; each time through, its constructor is called at the point of declaration, and its destructor is called at the end of the loop.This is nicer for at least three reasons:1) It means you are following the normally good practice of scoping your variables as tightly as possible.2) It means you don't have to call .close() yourself, because the destructor does that.3) It means you don't have to reset the file stream flags and make sure the stream refers to the beginning of the next file, because the constructor does that.Also, don't use .eof() for controlling a loop. It doesn't do what you want. .eof() will only return true after a read attempt has already failed. Thus, when the input file has just finished being read, .eof() is still false, and we loop again. This time, the read fails, and 'ch' is unchanged. The write succeeds, and writes a duplicate of the last ch-sized chunk. Then .eof() is finally true.You want to use the read operation itself to control the loop. Yes, this works. The library designers had to pull a little trick to make it work, but it's reliable and idiomatic and good.Finally, it's redundant to specify ios::out on ostreams or ios::in on istreams.ofstream outFile(outfile, std::ios::binary | std::ios::app);for (int i=0; i<3; i++) {    ifstream inFile(infile, std::ios::binary);    while (inFile.read(&ch,sizeof(ch))) { outFile.write(&ch,sizeof(ch)); }};  Thanks for the information Zahlman. I was using fstream objects, thats why I had the ios::in and out.   ;  I found this very useful for reading and writing with files:http://www.cplusplus.com/doc/tutorial/files/   