Reputation: 160
I'm a bit puzzled on how to manage file stream with fstream
. I'll order my main questions below:
1) First of all, why calling the constructors fstream file(file_name);
or fstream file; file.open(file_name);
without any flag like ios::in
or ios::out
doesn't create any new file?
2) Let's say i want to use the same file for input and output. I can do something like fstream file
and then call file.open(file_name, ios::out)
or file.open(file_name, ios::in)
when I need to use it for output or input operation separately. But what if I need to do input and output at the same time? For example, I need to read from a file and the replace a line or a character whenever I find a specific word or number while reading. Is calling file.open(file_name)
for an already created file (and without specifying any flag) possible? Does it allow me to do read/write operation with the same stream?
Upvotes: 1
Views: 193
Reputation: 57688
Here are my answers:
1) First of all, why calling the constructors fstream file(file_name); or fstream file; file.open(file_name); without any flag like ios::in or ios::out doesn't create any new file?
There is no need to create a file until a write operation occurs. This is delayed processing.
There is no need to create a file when the default may be to open the file with read permission. To my knowledge, opening a file for read-only never creates a file.
Read up on the default mode for those cases.
2) Let's say i want to use the same file for input and output. I can do something like fstream file and then call file.open(file_name, ios::out) or file.open(file_name, ios::in) when I need to use it for output or input operation separately. But what if I need to do input and output at the same time?
If you need to perform reading and writing to the same file, open it with both ios::in
and ios::out
modes: ios::out | ios::in
. This is informing the stream that you want to write and read using the same stream (file). This has been allowed at least since the C language was invented.
BTW, you should clarify "at the same time". Most everything is sequential on a computer, except when multiple processors (cores) are involved. Most files are sequential, meaning only one source can read or write to the file. Although there have been a few exceptions.
Many files have been treated as arrays. You can read and write the same section many times. This is called random access versus devices that are sequential access
.
Upvotes: 1
Reputation: 61
This is very doable using ifstream and ofstream:
#include <fstream>
using namespace std;
int main() {
ifstream source("source-file.txt");
ofstream destination("source-file.txt");
int x; // 0
int y = 1;
source >> x; // Reads one int from source-file.txt
source.close(); // Always close file when finished
destination << y; // Write to source-file.txt
return 0;
} // destination.close() called by destructor
Think of a stream like a straw. You can't drink and blow into the straw at the same time. Take a sip, stop, blow back out, repeat.
Upvotes: 1