Dealing with nonexistent file input

This is a discussion on Dealing with nonexistent file input within the C Programming forums, part of the General Programming Boards category; The command to activate the program would be: (C under linux) gcc program.c ./a.out infile outfile I'm trying to deal ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    Dealing with nonexistent file input

    The command to activate the program would be: (C under linux)

    gcc program.c
    ./a.out infile outfile

    I'm trying to deal with the user inputting a nonexistent infile. I tried

    Code:
    if (infile == Null){
      printf("file not found");
      return 0;
    }
    A book recommended using the "error function" which would be instead of print and return, I'd write <Error("file not found")> this throws an error during compiling though. "undefined reference to error." Maybe one of those books that assumes you included a library available on the books website, or maybe a C under linux thing...

    So far I either get a bunch of unreadable stuff, or a segmentation error.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    maybe try this:
    Code:
    FILE *infile, *outfile;
    char infilename[1024], outfilename[1024];
    
    ...
    
    infile = fopen(infilename, "r");
    if (infile == NULL){
        do_error_code();
    }
    
    ....

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    got it, just added exit(0). otherwise it would throw a segmentation error due to some other part of my code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 04:17 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21