如何在 C 中读取文件名中有空格的文件
How To Read a File That Has Spaces In Its Name in C
我试图打开一个文件来读取它的内容,但是当它的名称中有 space 时(比如 lot of spaces.txt
),它甚至都没有打开它。我怎样才能做到这一点?我在互联网上搜索但只找到反斜杠 \
解决方案(在每个 space [like 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] = '[=10=]';
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()
每个 space 只读取一个字符串 (" "),所以我改变了这个:
scanf("%s", directory);
对此:
getchar();
gets(directory);
//NOTE THAT THE USER NEEDS TO TYPE A DIRECTORY WITHOUT QUOTES ("")!
现在可以使用了。
我试图打开一个文件来读取它的内容,但是当它的名称中有 space 时(比如 lot of spaces.txt
),它甚至都没有打开它。我怎样才能做到这一点?我在互联网上搜索但只找到反斜杠 \
解决方案(在每个 space [like 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] = '[=10=]';
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()
每个 space 只读取一个字符串 (" "),所以我改变了这个:
scanf("%s", directory);
对此:
getchar();
gets(directory);
//NOTE THAT THE USER NEEDS TO TYPE A DIRECTORY WITHOUT QUOTES ("")!
现在可以使用了。