简体   繁体   中英

Reading and writing file streams simultaneously for two different files in C++

I am working on a project where I need to write a function to insert one image on top of another. Let's call this the insertion and original image (original image is the image to be inserted on). I am using C++ fstream.

So I would need to read the insertion image and write it into the original image. Both filestreams would be open, is there any informal standard that says I should not do this, ie, opening two files -- reading from 1 and writing to the other?

If you are reading the same file that you're writing, via a different stream object, then the actual results are implementation defined. You could end up overwriting parts of the file you haven't read yet, thus corrupting your input.

The most portable way to implement this would be to write the new content into a new file with a different filename, then after the entire process is complete, and both files are closed, rename the new file to the original filename.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM