如果没有给出,在 C++ 中在文件名中附加文件扩展名

Append the file extension in the file name if not given, in C++

我是 C++ 的新手。我想将 fileName 作为 command line argument。用户必须输入 fileName。如果用户输入 filename 而没有文件 扩展名 ,则程序应将 扩展名 添加到 fileName . 我正在 C++ 中编写代码。是否有功能或任何方法可以实现此目的?

if (!(fileName.find_last_of('.') != std::string::npos)) {
    fileName.append(".ext");
}