Thread: segmentation fault when opening a file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2023
    Posts
    5

    segmentation fault when opening a file

    hi guys! pretty new here, just wanted to ask if someone can save me another 5hrs of research - what could cause segmentation fault while i open a file?
    ps: it is in the same directory, and i tried entering whole path while starting program,.. nothing helps

    thank you A LOT

    here is the code:
    Code:
    int main(int argc, char *argv[])
    {
      char *file_name = argv[1];
      if (argc != 2) {
        printf("Usage: ./a4 %s\n", file_name);
        return 1;
      }
      FILE *config_file = fopen(argv[1], "r");
      if (config_file == NULL) {
        printf("Error: Cannot open file: %s\n", file_name);
        return 2;
      }
      printf("opened");
    
      fclose(config_file);
      return 0;
    }
    Last edited by Salem; 01-15-2023 at 12:07 AM. Reason: removed crayola

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading file in but receive segmentation fault
    By RozenKristal in forum C Programming
    Replies: 3
    Last Post: 09-29-2013, 07:08 AM
  2. Reading from file, segmentation fault
    By atac in forum C Programming
    Replies: 2
    Last Post: 05-08-2013, 10:34 PM
  3. Segmentation fault in Header file
    By Marc Oleson in forum C Programming
    Replies: 12
    Last Post: 04-26-2013, 05:39 PM
  4. Segmentation Fault. When trying to open file.
    By guyle in forum C Programming
    Replies: 5
    Last Post: 02-25-2013, 12:55 PM
  5. Segmentation fault with a file pointer?
    By Matt13 in forum C Programming
    Replies: 14
    Last Post: 07-31-2004, 05:53 AM

Tags for this Thread