Is it possible to input/output a C++ file through a variable in which the file name is stored instead of entering a specific file name?

Asked 1 years ago, Updated 1 years ago, 119 views

I'm a C++ beginner. Hello.

Not the same format as ifstream in ("C:\~~~~~\"Example.txt").

Stored as string A = "Example.txt",

I wonder if there is a way to write in the same format as ifstream in ("C:\~~~~\A").

I'm leaving a question like this because I can't find it even if I search on Google.

These are the ways that I tried to think about it, like ("C:\~~\Desktop\"G); ("C\~\Desktop"G); or Define. It's all an error.ㅜ<

I wanted to make a program that changes the contents of the file after receiving it, but it's too bad that it's blocked here.ㅜ<

c++ input output

2022-09-21 17:29

1 Answers

Create a new string with the + operator of string or
If it is an array of char, you can use the sprintf function.

#include <iostream>
#include <fstream>

using namespace std;
char input[512];

int main() {
    string file_name = "example.txt";

    ifstream inf("./" + file_name);
    inf.getline(input, 512);

    cout << input << endl;
    return 0;
}


2022-09-21 17:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.