Results 1 to 7 of 7

Thread: Test if file exists?

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Test if file exists?

    I am making a program with file I/O. How can I tell if a file exists before I write over it? (preferably non-Windows, but post that if it's the only way)

    Thanks
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Zaei
    Guest
    use fopen(), using an open only flag. If your pointer comes back as NULL, the file doesnt exist.

    Z.

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    What is an open only flag? I am doing this right now and it tells me it exists no matter what.
    PHP Code:
    FILEpFile fopen(fname"r");
    if (
    pFile == NULL)
    {
        
    printf("File %s already exists "fname);

    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    Zaei
    Guest
    The file would exist if the pointer is NOT NULL. Try this:
    Code:
    FILE* f = NULL, fopen(fname, "r");
    if(f)
    {
        // file exists
    }
    Z.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Wynd:
    if (pFile == NULL)
    {
    printf("File %s already exists ", fname);
    }
    If the file cannot be opened fopen returns NULL. So if pFile is NULL it means that the file couldn't be opened. This is surely not because it already exists, but rather because it doesn't exist. You are drawing the false conclusion from the fact that pFile is NULL. Your program worked all the time, it told you that the file exists only because you told it to say the wrong thing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Ok, I see now

    Thanks for the help
    Alcohol & calculus don't mix.
    Never drink & derive.

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    How about this?

    PHP Code:
    hFile CreateFile(lpszFileName,
                
    GENERIC_READ,
                
    FILE_SHARE_READ,
                
    NULL,
                
    OPEN_EXISTING,
                
    FILE_ATTRIBUTE_NORMAL FILE_FLAG_RANDOM_ACCESS,
                
    NULL);
        
    if (
    hFile == INVALID_HANDLE_VALUE || hFile == NULL)
    {
        
    // File does not exist
        
    CloseHandle(hFile);
        return 
    0;
    }
    else
        
    // File exist
        // Set return value
        
    return 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width