0

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, i.e., opening two files -- reading from 1 and writing to the other?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No I'm not reading and writing to the same file. So let's say I have already read the original image and stored it in some variable 'originalImage'. I want to simultaneously read the insertion image and modify the 'originalImage' variable and then write it to a different file.
In that case there is no issue. Applications read and write multiple files, concurrently, all the time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.