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

标签 c file spaces

我试图打开一个文件来读取其内容,但是当文件名称中包含空格时(如 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/

相关文章:

c++ - 格式化值类型 float(编程语言 C/C++)

c - 文件写入C困惑

C编程基本回调函数

linux - 为多个用户分配 Linux 文件权限

performance - 如何有效地在文件中插入 byte slice ?

c - 可编辑 GtkTreeView 的困难

java - 当您打开文件时,什么会发送到应用程序?

c - 硬编码指针值

bash - 终端将命令应用于所有子目录

java - 计算空格的问题 - Java