我试图打开一个文件来读取其内容,但是当文件名称中包含空格时(如 lot ofspaces.txt
),它甚至无法打开它。我怎样才能做到这一点?我在互联网上搜索但只找到了反斜杠 \
解决方案(在每个空格前添加反斜杠[如lot\of\spaces.txt
]),这不起作用对我来说。
MyFileCompressor.c
int main()
{
char directory[100];
char * direct;
printf("File: ");
scanf("%s", directory);
if((direct = malloc(strlen(diretorio)+strlen(".newextension")+1)) != NULL)
{
direct[0] = '\0';
strcat(direct, directory);
strcat(directory,".newextension");
}
else
{
printf("Error!\n\n");
return;
}
compress_file(directory, direct); //compress the file in typed directory to the new directory (direct)
return 0;
}
最佳答案
scanf()
每个空格 ("") 只能读取一个字符串,因此我对此进行了更改:
scanf("%s", directory);
对此:
getchar();
gets(directory);
//NOTE THAT THE USER NEEDS TO TYPE A DIRECTORY WITHOUT QUOTES ("")!
现在可以工作了。
关于c - 如何在 C 中读取名称中包含空格的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58965988/