P. Nawin
P. Nawin

Reputation: 53

warning: passing 'const char *' to parameter of type 'char *' discards qualifiers

int main(int argc, char const *argv[])
{
    char original[5] = ".txt";
    int c;
    strcat(argv[1], original);
    FILE *find;
    find = fopen( argv[1], "r");
    if (find) {
        while ((c = getc(find)) != EOF)
            putchar(c);
        fclose(find);
    }

    return(0);
}

What will I do? Now, i creating a project with c programming.

Upvotes: 3

Views: 4688

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50774

strcat(argv[1], original);

The type of argv[1] is const char*, and the first argument of strcat is char*.

The strings in argv are not supposed to be modified.

You basically need this (there is still room for improvement):

char txtextension[5] = ".txt";
char filename[200];  // 200 will be hopefully enough 
strcpy(filename, argv[1]);
strcat(filename, txtextension);
FILE *find;
find = fopen(filename, "r");

Sidenotes:

  1. Replace char original[5] = ".txt"; with char original[] = ".txt";. In the latter case the compiler automatically allocates the correct memory size.
  2. Name variables according to what they represent, so original should be named for example txtextension.

Upvotes: 2

Related Questions