Opening a file then closing does not release file lock - c++

I have some c++ code in a node.js function that opens a file like this and reads it into a buffer and then closes it.
ifstream inputFile(source.c_str(), ios::in | ios::binary);
inputFile.read(buffer, results.st_size);
inputFile.close();
Then I manipulate the file in some way and attempt to write it back to the same location. I get the following error code and message: "Text file busy". I know there is no other process touching the file. If I rename the file to some random name, and rename it back, then I am able to overwrite the file.

Related

How to read contents from file and rewrite contents into it in qt?

I want to read contents from a file, then do some processing, rewrite processed contents into that file.
But after I open file in QIODevice::Truncate mode, then the initial content is empty, but I need initial contents:
QFile update_a_file("some/path/to/file.txt");
update_a_file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate);
QString fileContents(update_a_file.readAll());
qInfo("trial info %s", fileContents.toStdString().c_str());
There is nothing from the output log.
So, how can I read contents, and rewrite(empty initial contents and write new processed contents) into file in one open call? Or are there some other file mode in qt?
You are erasing the contents of the file when you open it with QIODevice::Truncate.
From the docs:
If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
Try taking these steps:
Open the file without Truncate.
Read the content
Close the file
Open the file again, this time with Truncate
Write your modified content.
Close the file.

how to Read/Write multiple objects from file in C++?

When i try this writing multiple objects to file is done properly.
but while i try to Read multiple objects from file it show only data of last object.
Write Multiple objects to file:-
for(int i=0;i<n;i++)
{
s[i].getdata();
ofstream file("Simple.txt");
if(file.is_open())
{
cout<<"File created"<<endl;
file.write((char*)&s[i],sizeof(s[i]));
file.close();
}
else
{
cout<<"File not Open../"<<endl;
}
}
where, s is object
used in for loop as generating multiple objects.
When you execute ofstream file("Simple.txt"); you are opening a file in write mode.
And that you are doing for every iteration.
It means every time you write a file it is writing from beginning. ie, you are overwriting your file everytime.
You should try opening in append mode.
That might help.
Also as the first comment suggested, it is not a good idea to open and close a file in every iteration.
Hope this helps.
...
std::ofstream file;
file.open("Simple.txt" | std::ofstream::app);
...
You have to open the file in append mode.
You could also declare the file handle before the loop, and then just read in the values, then after the loop close the file handle.

How to clear a file in append mode in C++

I've a file on which I require multiple operations. Sometimes I just want to append data at the end of the file, sometimes I just want to read from the file, and sometimes, I want to erase all the data and write form the beginning of the file. And then, I again need to append data at the end of file.
I'm using following code:
ofstream writeToTempFile;
ifstream readFromTempFile;
writeToTempFile.open("tempFile.txt", ios::app | ios::out);
readFromTempFile.open("tempFile.txt", ios::in);
// Reading and Appending data to the file
// Now it is time to erase all the previous data and start writing from the beginning
writeToTempFile.open("tempFile.txt", std::ofstream::trunc); // Here I'm removing the contents.
// Write some data to the file
writeToTempFile.open("tempFile.txt", std::ofstream::app); // Using this, I'm again having my file in append mode
But what I've done doesn't work correctly. Please suggest me some solution in C++. ( Not in C)
The problem with the code is:
I wasn't closing the file before I called the method open again on it.
So, close the file before you re-open it with some different permissions.

File handling C++ , opening in ios::app mode, file getting wiped

I am making a programme in turbo C++ which requires input and output to a file as well as retrieving the file data for later use.
fstream file("playlist.txt",ios::in|ios::app);
this is what I use but everytime i close the programme and run it again, all the previous contents get wiped. I thought ios::app "Sets the stream's position indicator to the end of the stream before each output operation".
I tried ios::nocreate also, but to no effect.
You should have fstream::out too:
fstream file("playlist.txt", fstream::in | fstream::out);
Then you can move file pointer to end of file. in and app can't be used together: you need to open for R/W because app implies you won't read.

is there any way to clear the content of a text file dynamically in c++?

I am using ofstream to output some text to a file in ios::app mode within a loop. But after some step, I need to clear the content of the file. I know we can do it by either delete the file and open again or to open it again with ios::trunc, but is there any where I can get it done without close and open the file again?
If you have opened it in ios::app mode, there's no way to clear content without opening it again. ofstream can only put text in a file, and as text files are sequential, you can't directly erase data on them.
Note sure if it is possible with io streams, but in general you can truncate an open file by setting its current position to 0 and then setting the EOF marker on the file. In the Win32 API, for instance, you can do that with SetFilePointer() and SetEndOfFile().