如何在 C 中读取名称中包含空格的文件

难以置信的编码

我试图打开一个文件来读取它的内容,但是当它的名称中有空格时(如lot of spaces.txt),它甚至不会打开它。我怎样才能做到这一点?我在互联网上搜索,但只找到了反斜杠\解决方案(在每个空格前添加一个反斜杠 [like lot\ of\ spaces.txt]),这对我不起作用。

我的文件压缩器

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 ("")!

现在它正在工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章