What happens when the output file is already there and is empty
In this blog, all file-related operations have been asked. If a file already exists for a particular current output file that is created, the file contents can be discarded. By using a common string data type and then converting to desired datatypes any data type can be read. A file can also be appended using ios::base.
QUESTION
a) What happens when the output file is already there and is empty? (c++)
Answer:
In C++, if an output file is created and if it is already present with the same name in the current directory, all the file contents will be discarded and will be made empty for it to be used.
b) What happens if the file is not empty?
Answer:
If the file is already empty, there will not be any discarding required as it can be used for the current output.
c) How can you check if the file is not empty and write only if empty?
Answer:
To check if the file is empty or not:
#include<iostream> #include<fstream> using namespace std; int main(){ ifstream in("data.txt"); if(in.peek() == EOF) cout<<"Empty"<<endl; else cout<<"Not Empty"<<endl; }
d) How can you append to the file?
Answer:
To append any data to an existing file, the ios::app can be used which opens a file in append mode.
Example :
#include<iostream> #include<fstream> using namespace std; int main(){ ofstream out; out.open("data.txt", ios::app); out<<"Some data"; }
e) Can you use a single inFS object to read all values?
Answer:
Yes, We can use one input file stream object to read all the values from a file.
f) If there are more values than the ones you are trying to read?
Answer:
On encountering the first unwanted value, we can stop reading.
g) What if there are more integers than three?
Answer:
We can read until the end of the file and also can stop reading whenever we want to stop reading. If one wants to read any 3 integer values, a break can be given after 3 iterations of reading the file.
h) What will happen if there is a character inserted?
Answer:
If a character is present instead of an integer, the file can be read as a string datatype and then can be converted into the desired datatype.
i) What if there are double values?
Answer:
The same answer as above. Read all the contents as a string and then convert to the specific datatype of it.
j) What if you try to read more values than are in the file? Extra Value gets 32767
Answer:
On using the EOF keyword of C++, we can’t read extra. One can read-only until the end of the file.
k) Can you read double values?
Answer:
Double values can be obviously read.
i) What would happen if you have a double value followed by a char and then a string in the file and try to read an integer, followed by a char, followed by a string?
Answer:
Read the file as either a character array or string first, and then convert them to specified datatypes like int, f=double float as desired.
m) If you are trying to read a string and the string in the file is: _is_a_genius, and there is a space in place of the first, leading _, will that space be a part of the string?
Answer:
There are two kinds of methods to read in c++:
a) One method can read line by line from the file(includes spaces and tabs).
b)The other method reads the file word by word, by considering spaces i.e the spaces won’t be included as part of the string in the given case.
n) If all _ are replaced by spaces in the above string in the file, what will be read into the target string?
Answer:
If the file is read word by word, then the target word will have only “is”.
o) Is it a recommended practice or is it a must that you must close a file that was opened?
Answer:
It is a recommended practice to close a file always when you use it.