Thread: Checking if a file exists

  1. #1
    Registered User
    Join Date
    Mar 2012

    Checking if a file exists

    If my program asks a user to input a file name and then check if that file exists, would I have to use an if statement to see if that file is able to open?

    Something like this:

    if(FILE_1 = fopen(nameoffile,"r") == NULL){
                FILE_1= fopen(nameoffile, "w");    
    When I tried this, it resulted in a warning
    C4047: '=' : FILE * differs in levels of indirection from 'int'

    My program still works. I've used a similar line of code in other programs without having this warning.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Nice, France
    I would say yes,because there the file may can not be accessed due to restricted rights or something else .
    Check here for ref of fopen fopen - C++ Reference

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    The way you have that written certainly should not work.
    = and == differ on precedence.

    What happens in the if statement is
    if (FILE_1 = (fopen(nameoffile, "r") == NULL))

    Not the correct
    if ((FILE_1 = fopen(nameoffile, "r")) == NULL)
    Yes, the parentheses are required.

    The comparison has to reduce down to a 0/1 (they introduced true/false in C99 with the bool type) which is where the conversion to int, and the warning, comes from.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Yet another case of the question asked (can I check if a file exists using fopen()?) being completely unrelated to the actual problem (I think the code works but why doesn't it compile?).

    So I'll address the actual problem in the code, not the question asked.

    You probably have more brackets in the working version. Assignment is lower precedence than a comparison, so your code is actually equivalent to
    if (FILE_1 = [color="RED"](fopennameoffile, "r") == NULL)){
            FILE_1= fopen(nameoffile, "w");   
    The if() statement will only compile without error/warning if FILE_1 is an integral type. The body of the if() block will only compile without warniong if FILE_1 is of type FILE * (or a void pointer). Those two conditions for your code to compile are mutually exclusive.

    You presumably intended something like
    if ((FILE_1 = fopennameoffile, "r")) == NULL){
            FILE_1= fopen(nameoffile, "w");   
    Notice how the brackets I have highlighted in red are in different places.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking if a Bash variable exists?
    By cpjust in forum Tech Board
    Replies: 0
    Last Post: 03-25-2009, 03:00 PM
  2. Checking if file exists?
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 08-16-2007, 04:06 PM
  3. Checking if a path exists
    By Cmaniac in forum Linux Programming
    Replies: 3
    Last Post: 04-02-2007, 07:58 AM
  4. Checking if a window (by title!) exists?
    By willkoh in forum Windows Programming
    Replies: 2
    Last Post: 04-02-2005, 06:26 AM
  5. Checking to see if a file exists...
    By Util_Mark in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 04:01 PM