C assign string from argv[] to char array

24,115

You must copy the string into the char array, this cannot be done with a simple assignment.

The simplistic answer is strcpy(filename, argv[1]);.

There is a big problem with this method: the command line parameter might be longer than the filename array, leading to a buffer overflow.

The correct answer therefore:

if (argc < 2) {
    printf("missing filename\n");
    exit(1);
}
if (strlen(argv[1]) >= sizeof(filename)) {
    printf("filename too long: %s\n", argv[1]);
    exit(1);
}
strcpy(filename, argv[1]);
...

You might want to output the error messages to stderr. As a side note, you probably want to choose English or German, but not use both at the same time ;-)

An even simpler solution is to just keep a copy of the pointer argv[1] in a char *filename. Unless you modify it yourself, a very bad idea, its contents will not change for the duration of the program execution.

Here is a modified version:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
    FILE *datei;
    char *filename;

    if (argc < 2) {
        fprintf(stderr, "Fehlendes Dateiname-Befehlszeilenargument\n");
        return EXIT_FAILURE;
    }
    filename = argv[1];
    datei = fopen(filename, "r");
    if (datei != NULL) {
        printf("Datei erfolgreich geöffnet\n");
    } else {
        fprintf(stderr, "Fehler beim öffnen von %s: %s\n",
                filename, strerror(errno));
        return EXIT_FAILURE;
    }
    // ...
    fclose(datei);
    return EXIT_SUCCESS;
}
Share:
24,115
danielr1996
Author by

danielr1996

Updated on May 21, 2022

Comments

  • danielr1996
    danielr1996 almost 2 years

    I have the following code which reads an file name from the command line and opens this file:

    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char **argv){
        FILE *datei;
        char filename[255];
    
        //filename = argv[1];
        //datei=fopen(filename, "r");
        datei=fopen(argv[1], "r");
        if(datei != NULL)
            printf("File opened");
        else{
            printf("Fehler beim öffnen von %s\n", filename);
            return EXIT_FAILURE;
        }
        return EXIT_SUCCESS;
    }
    

    This example works, but I want to write the string from the command line to the char array and pass that char array to to fopen(), but i get the compiler error Error: assignment to expression with array type filename = argv[1];

    What does this error mean and what can I do to fix it?

    • R Sahu
      R Sahu about 9 years
      Use strcpy(filename, argv[1]);
    • R Sahu
      R Sahu about 9 years
      possible duplicate of Cannot assign an array to another
    • chqrlie
      chqrlie about 9 years
      add a \n at the end of the "File opened" message. Use braces around this instruction for consistency with the else clause.
    • harper
      harper about 9 years
      Use strncpy(filename, argv[1], sizeof filename);
  • chqrlie
    chqrlie about 9 years
    A simpler solution is to just keep a copy of the pointer argv[1] in a char *filename. Unless you modify it yourself, a very bad idea, its contents will not change for the duration of the program execution.
  • S3DEV
    S3DEV almost 2 years
    Just a clarification on the excellent suggestion by @chqrlie - it would look like this: const char *filename = argv[1];.