Creating file names automatically C++

前端 未结 5 1436
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 18:41

I\'m trying to write a program in C++, which creates some files (.txt) and writes down the result in them. The problem is that an amount of these files is not fixed at the begin

5条回答
  •  自闭症患者
    2021-02-04 19:03

    You can get a const char* from an std::string by using the c_str member function.

    std::string s = ...;
    const char* c = s.c_str();
    

    If you don't want to use std::string (maybe you don't want to do memory allocations) then you can use snprintf to create a formatted string:

    #include 
    ...
    char buffer[16]; // make sure it's big enough
    snprintf(buffer, sizeof(buffer), "file_%d.txt", n);
    

    n here is the number in the filename.

提交回复
热议问题