Fstream won't create file after remove() function - C++ - c++

I've an fstream file and after I've used remove("file.txt") - which is path of the file created using file.open() - I wanted to create this file again with other content, but the file won't create. What's causing the problem?

Related

What is an fstream object? An object to output to files and not actually the file stream or does it represent the file stream?

fstream represents all the input and output file streams right? When I create an ofstream object, am I creating an object that can be used to output to files (doesn't actually represent the file stream) or am I creating a file stream? When I call the function std::ofstream::open, it creates the stream from the ofstream object to the file I specify right?
"File stream" is a concept. You create a "stream" when you create an object of std::ofstream but it's not neccessarily associated with a file straight away. You can "associate" it either upon creation (by specifying the file as a constructor argument) or as a separate operation with use of std::ofstream::open

Can I change the values of a FILE variable without saving the file?

I have a txt file that I want to change all characters.
So I'm opening it with this line:
FILE *myFile = fopen(filename, "rb");
The rest of the program uses myFile variable after loaded.
My goal is: crypt this file with another program and decrypt when I load without messing with the rest of the code and keep the original file crypted.
Is there a way?

ifstream file opening issues

I am having a hell of a time trying to open files on a network drive with ifstream.
I can successfully open the file if... I explicitly declare the filename, such as ifstream f("filename.txt").
However, that is the only way I can get the file open, and I need be able to dynamically find the name of that file and open it. Right now I have a string vector of the filenames in a given folder.
I have tried the following as input arguments to the ifstream constructor, to no success.
converting the string using c_str().
declaring a char* and assigning the string.cstr() to it.
same as above, but const char*
changed the system directory to the folder where the file is, and inputted the filename itself (relative path)
I print out the filename and change to its directory each time before trying to open it, so I know for a fact the filename is 100% correct.

Opening a file then closing does not release file lock

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.

Unable to create a temporary file on Windows in C++ using tmpfnam

I am trying to create a temporary file in my C++ program by calling the tmpfnam function to get the temporary file name and using that to create the file for writing, but my code is unable to create the temporary file. However, file creation works absolutely fine when I use a user-supplied file name or a string constant instead of using a temporary name from tmpfnam. Here is an example :
std::tmpnam(fname); //does not work
std::fstream f(fname,std::ios::out);
char* fname = "myfile.txt"; //works
std::fstream f(fname,std::ios::out);
I checked that the file in case 2 is being created in the same directory as the source file, but not in case 1. I tried running the program under admin permissions as well and still no luck . Any idea why this is so ?
Have you tried it this way?
char fname[L_tmpnam];
if (std::tmpnam(fname)) {
std::fstream f(fname,std::ios::out);
// ...
}
Hope that it helps.