mardi 26 mai 2020

Why does ios::trunc file access mode not work in the ifstream class when I want to open the file?

I knew that ios::trunc is file access mode that works If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.

When i use it for file write operation(ofstream class), it works normally. But, when i use it for file read operation(ifstream class), it doesn't delete the previous content in a file and the content of the file keep unchanged at all.

Suppose i have test.txt file:

Hello Guys..

And this is my code for opening the file:

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

int main(){

    ifstream inputFile("test.txt",ios::in|ios::trunc); //This opens test.txt file and does not delete the contents.
    inputFile.close();

    ofstream outputFile("test.txt",ios::out|ios::trunc); //This opens test.txt and delete the all the contents.
    outputFile.close();

    return 0;
}

Why is this happening?.... Thank you in advance...

Aucun commentaire:

Enregistrer un commentaire