我想打开从命令行发送的文件名,但该文件位于/home/docs/cs230 中。下面是我试过的代码,但是当我试图在 linux 中编译时它显示错误:
int main(int arg, char* args[1]) {
// Open the file
newfile = fopen("/home/docs/cs230/"+args[1], "w+b");
}
最佳答案
因为这是 C++,我们可以像这样使用 std::string
:
int main(int arg, char* args[]) {
// Open the file
std::string path( "/home/docs/cs230/" ) ;
path+= args[1] ;
std::cout << path << std::endl ;
FILE *newfile = fopen( path.c_str(), "w+b");
}
Mats 也做出了很好的评论,在 C++ 中我们将使用 fstream ,您可以在链接中阅读更多信息。
关于c++ - 从服务器中不同位置的命令行参数传递的打开文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16027133/