-2

I'm trying to open a file to read its content, but when it has spaces in its name (like lot of spaces.txt), it doesn't even open it. How can I do that? I searched on the internet but only found the backslash \ solution (add a backslash before every space [like lot\ of\ spaces.txt]), that doesn't works to me.

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;

}
IncredibleCoding
  • 191
  • 2
  • 14

1 Answers1

2

scanf() only reads one string per space (" "), so I've changed this:

     scanf("%s", directory);   

to this:

    getchar();
    gets(directory);

    //NOTE THAT THE USER NEEDS TO TYPE A DIRECTORY WITHOUT QUOTES ("")!

Now it's working.

IncredibleCoding
  • 191
  • 2
  • 14