I need some help with Binary I/O. I know how to do it when I am specifying the filename in the code
ie: fstream fio("persons.txt", ios::in | ios::out | ios::binary | ios::trunc); What I need to know is how to open or create a file with a file name that will vary and wont be persons. txt everytime. Basically, a file name specified by the user. How would I do that? I can't seem to find anything online for it. Also, please dumb it down for me, I'm still kind of new to this.

Dani AI

Generated

asked how to let the user pick a filename instead of hardcoding it. Early replies from , and pointed out the basic idea (use a variable; older compilers expect a C-style string). Below is a concise, practical pattern that handles filenames with spaces, creates the file if needed, and checks for open errors — using modern, safe idioms.

#include <iostream>
#include <fstream>
#include <string>

std::string filename;
std::getline(std::cin, filename);            // accepts spaces

std::fstream fio;
fio.open(filename, std::ios::binary | std::ios::in | std::ios::out);

if (!fio.is_open()) {                        // create if it didn't exist
    std::ofstream create(filename, std::ios::binary);
    create.close();
    fio.open(filename, std::ios::binary | std::ios::in | std::ios::out);
}

if (!fio) {
    std::cerr << "Unable to open file: " << filename << '\n';
    // handle error (permissions, invalid path, etc.)
}

Notes and troubleshooting tips: operator>> stops at whitespace, so prefer std::getline for filenames with spaces. Including std::ios::out (or using std::ofstream) creates the file; std::ios::trunc will erase contents; std::ios::app appends. Always test .is_open() or .fail(), or set exception masks with .exceptions(...) for stricter error handling. For portability and Unicode paths prefer std::filesystem::path (C++17+).

Cautions for binary I/O: avoid writing raw in-memory structs directly (padding and endianness cause portability problems). Use explicit serialization (defined byte layout or a binary format) for stable storage. Also validate/sanitize user-supplied paths and check directory existence and permissions before opening.

Recommended Answers

All 3 Replies

Replace "persons.txt" with a string variable containing the filename you have gotten from the user. I hope you know how to get user input...

By the way, code tags should look like this:
&#91;code=c++&#93; ...your code... &#91;/code&#93;
The fact that you tried is appreciated, however :P

You will need to use the c_str() function of the string because a lot of these function accept an array of characters, not an actual string.

#include <string>
using namespace std;

string filename = "whatever.txt";
fstream fio(filename.c_str(), ios::in | ios::out | ios::binary | ios::trunc);

Dave

If you are using C++ <string>

string s1;
cin>>s1;
fstream fio(s1.c_str(), ios::in | ios::out | ios::binary | ios::trunc);

But if you are using an old compiler, Like Turbo C++(although, I recommend you to switch to a new compiler, Read the Article :http://cppdb.blogspot.com/2008/10/why-you-shouldnt-use-you-use-old-c.html)

char s1[50];
cin>>s1;
fstream fio(s1, ios::in | ios::out | ios::binary | ios::trunc);

Please Mark the thread as Solved:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.