Does 'fstream' always open files from the beginning?

HI!
I was wondering if I open a file (for example "fstream myfile ("example.txt" , ios::in) ;") does it always go at the beginning of the file ?
I mean doesn't any other program (for example a text editor) affect it?
What about another function in another thread which is running simultaneously and using the same file ?

Do i need to do a myfile.seekg (0) after I open my file to make sure my file starts at the beginning?
Thanks!
does it always go at the beginning of the file ?

Why shouldn't it?

I mean doesn't any other program (for example a text editor) affect it?

What do you mean?

What about another function in another thread which is running simultaneously and using the same file ?

It's not a problem, because you'll be opening the file with a different fstream instance. Even if you were to somehow open the same file using the same fstream instance, it wouldn't matter. You can open any file as many times as you want. How many times or if you load a file into memory more than once at the same time has no bearing on the file.
Last edited on
Thanks @xismn ;
But I mean if there are 2 threads , running 2 different functions and both of them open the same stream .
So them both read the file into buffer using the cursor they have .
This cursor points a particular position in file . So There are 2 cursors in the file (one in a function in a thread , and one in another function in another thread)
I'm just wondering if C++ can handle 2 cursors for a file and one cursor doesn't affect the other's ?

Sorry if my English is bad , If still need more description , let me know .
Thanks !~
What you're describing is not possible with file stream objects, unless I am misunderstanding what you're saying.

If you open the same file with the same stream in two different threads at the same time, the second of the two calls to fstream.open() will fail and set the failbit.

Straight from the cplusplus.com reference:
If the stream is already associated with a file (i.e., it is already open), calling this function[(fstream.open())] fails.
Yeah Thanks !
Got it .
Do External apps affect it too ?
like Notepad , or another application using it ?
Do External apps affect it too ?

Some do, yes.

Some applications, such as MS Word or Excel, can block any other application from attempting to open the file at the same time. But others may not be a problem.
Topic archived. No new replies allowed.