How to set filename parameter as actual filename - c++

I'm wondering how I can set a filename which my function receives as a parameter set as an actual filename.
My Syntax is
std::ofstream file("filename.bin",
std::ios::out | std::ios::trunc | std::ios::binary);
But that will name the file "filename.bin" ofc.
How would I have to write it if I get the filename as parameter and save the filename in a string called SavedFilename. And name the file after the string instead of "filename.bin".
Thanks.

You mean like this?
void foo(const char* filename) {
std::ofstream file(filename,
std::ios::out | std::ios::trunc | std::ios::binary);
// ...
}
foo("filename.bin");

Related

Opening a file to read and write, create it if it doesn't exist

I was trying to create a file in both read and write modes, but it doesn't create the file, what can be the problem?
This is code:
fstream file("NameFile.txt", ios::out| ios::in);
The program will start, but it will not create any files.
When you open the file using fstream:
To read, the file is required to exist;
To write you need to specify a write mode, ofstream would do that for you, but with fstream you need to do it yourself:
Replaces the contents of the file when you write (ofstream default mode).
std::fstream file("NameFile.txt", std::ios::out | std::ios::in | std::ios::trunc);
^^^^^^^^^^^^^^^
Appends to the existing data in the file when you write.
std::fstream file("NameFile.txt", std::ios::out | std::ios::in | std::ios::app);
^^^^^^^^^^^^^
Note that after reading or writing you'll need to set the offset position in the file, for instance:
std::string s = "my string";
std::string in;
file << s;
file >> in;
file >> in will not read anything, the position indicator is at the end of the file after file << s, you'll need to reset it if you want to read previously written data, for example:
file << s;
file.seekg(0);
file >> in;
This resets the read position indicator to the beggining of the file, before the file is read from, more about it here:
https://en.cppreference.com/w/cpp/io/basic_fstream
well, you initialized an object, now to create a file use
file.open();
see
fstream won't create a file

How to overwrite a file with fstream

I have 2 fstream objects called oldFile and newFile and I want to find the non-Null elements in oldFile and write them into newFile of a destructor. The name of the file would be exactly the same "file.txt" so I am essentially overwriting oldFile with newFile.
~Destructor(){
T foo;
fstream newFile;
oldFile.open(fileName, ios::in | ios::out | ios::binary);
oldFile.clear();
while(!oldFile.eof()){
foo.readFromFile(oldFile);
if(!foo.isNull()){
foo.writeToFile(newFile);
}
}
}
Where do I go from here? How do I name the newFile object to "file.txt" and make sure that the oldFile object is overwritten? Very basic file I/O, don't need anything too fancy.

File object with fstream object is not created

I am trying to create a binary file as follows:
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Hello World";
fstream fileObj = std::fstream("test_File.db", std::ios::in | std::ios::out | std::ios::binary);
if(fileObj)
std::cout<<"success";
else
std::cout<<"fail";
return 0;
}
But fileObj is not created and always else part is executed. Please guide if I am missing anything.
A stream opened with in | out | binary does not create a file that does not exist. You should get into the habit of reading the documentation!
Try in | out | app | binary (assuming you want existing contents to be kept; also get into the habit of clearly stating your goal/requirements).
And there is no need to initialise from a temporary like that; just instantiate the object in the usual manner, e.g.
std::fstream fileObj(
"test_File.db",
std::ios::in | std::ios::out | std::ios::app | std::ios::binary
);

Having trouble outputting file [duplicate]

I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
std::cout<<"error"<<strerror(errorno);
I get this result:
"No such file or directory."
How can I create the file in this case?
You're specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file.
Either remove std::fstream::in from your mode argument, or specify std::fstream::trunc in addition to the other flags.
It's a little messy but works. Doesn't overwrite the file if it exists but creates a new one if the first open fails.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
{
my_stream.open("my_file_name",std::fstream::binary | std::fstream::trunc | std::fstream::out);
my_stream.close();
// re-open with original flags
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
}
else
{
// read something
}
// read/write here

std::fstream doesn't create file

I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
std::cout<<"error"<<strerror(errorno);
I get this result:
"No such file or directory."
How can I create the file in this case?
You're specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file.
Either remove std::fstream::in from your mode argument, or specify std::fstream::trunc in addition to the other flags.
It's a little messy but works. Doesn't overwrite the file if it exists but creates a new one if the first open fails.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
{
my_stream.open("my_file_name",std::fstream::binary | std::fstream::trunc | std::fstream::out);
my_stream.close();
// re-open with original flags
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
}
else
{
// read something
}
// read/write here