Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and also read c++ fstream file without closing it

Tags:

c++

file

fstream

I opened a file both read and write mode

using the following statement

file.open(fileName, ios::in | ios::out | ios::trunc);

my main purpose for opening the file in both mode is, read and write the file at the same time.

But In my code scenario,

when I am reading the file after writing it, the ouput showing blank that means, it is not saving my writing contents because I am not closing it.

And I want to close the file after finishing both write and read the operation

I found a solution in Stack Overflow,

to use flush() function to save the file without closing

file.flush();

but, the problem is it's not working for my case

So, how can I save c++ fstream file without closing?

Here's my full code for better understanding

#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main(int argc, char const *argv[])
{
    string fileName = "text.txt";

    fstream file;


    file.open(fileName, ios::in | ios::out | ios::trunc);

    if (file.is_open())
    {
        file << "I am a Programmer" << endl;
        file << "I love to play" << endl;
        file << "I love to work game and software development" << endl;
        file << "My id is: " << 1510176113 << endl;

        file.flush(); // not working 
    }
    else
    {
        cout << "can not open the file: " << fileName << endl;
    }

    if (file.is_open())
    {
        string line;

        while(file)
        {
            getline(file, line);

            cout << line << endl;
        }
    }
    else
    {
        cout << "can not read file: " << fileName << endl;
    }

    file.close();

    return 0;
}
like image 998
SHAH MD IMRAN HOSSAIN Avatar asked Nov 18 '18 10:11

SHAH MD IMRAN HOSSAIN


People also ask

Does fstream automatically close?

When the fstream/ifstream/ofstream object is out of scope, the file will be closed automatically. However, you can explicitly call the close() function to close it and release the resource if the fstream object will not be out of scope for a while.

Do I need to close fstream?

fstream is a proper RAII object, it does close automatically at the end of the scope, and there is absolutely no need whatsoever to call close manually when closing at the end of the scope is sufficient. In particular, it's not a “best practice” and it's not necessary to flush the output.

How do I save a file in C C++?

cpp." In Notepad, click the “File” menu and select “Save As.” When the Save As dialog appears, change the file type to “All Files,” name the file “hello. cpp” and click the “Save” button.


2 Answers

Actually, if you want to save any file immediately without closing the file, then you can simply use

file.flush();

But if you are want to read the file without closing just after writing it, you can simply use

file.seekg(0);

actually seekg() function resets the file pointer at the beginning, for this, it is not mandatory to save the file. so, this has nothing to with flush() function

but you can do both if you want to

like image 129
Nazmul Haque Avatar answered Nov 15 '22 07:11

Nazmul Haque


Before reading from that file you need to make sure to place the pointer to that file to the beginning of the file. After writing to the file it'll point to the end. Therefore you won't be able to read anything.

You need to use file.seekg(0); somewhere after the file.flush() but before starting to read to place the file pointer to the very beginning.

Update

This should work without the flush. However this will depend on the implementation of the std library. Although I'd consider this as bug if it doesn't work without calling flush() imho it does not hurt to call it explicitly.

like image 24
Joerg S Avatar answered Nov 15 '22 07:11

Joerg S