3 Replies - 1885 Views - Last Post: 20 July 2011 - 01:57 AM Rate Topic: -----

#1 DamionM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-July 11

How to check if a file exists with out opening or creating a file

Posted 19 July 2011 - 08:13 PM

I am working on an assignment for my programing class writing a program that is basically a cash registrar. I am writing a part that asks an employee to enter his/her identification number as a string then checks to see if there is a file with that identification number the number being the name of the file. if the file exists it should open the file and read the contents and use it to construct an employee class object. if it doesn't exists it will prompt the employee to create a new file.

Simply put I want to know if there is a way to check if the file exists without creating a new file or a good way to do what I described above. if you could provide an example of how to do it using something maid up so I can have see it too that would be nice.

Thank you to anyone that helps :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to check if a file exists with out opening or creating a file

#2 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 90
  • Joined: 04-July 11

Re: How to check if a file exists with out opening or creating a file

Posted 19 July 2011 - 08:28 PM

without opening it?..

i'm not a C++ coder so this function may have another equivalent or better one in C.. but i think the concept still remains..

use fopen. fopen checks if there is a specified file, if it does not exist, of course it does not open the file and returns NULL..

if ( (fp = fopen( ur_filename_here, "r" )) == NULL )
     printf("File not found");



it depends in what mode in opening your are using..
example modes..
r = for reading
w = for writing..
and other modes..
Was This Post Helpful? 0
  • +
  • -

#3 dcomm  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 09-June 11

Re: How to check if a file exists with out opening or creating a file

Posted 19 July 2011 - 08:44 PM

You can do this by checking the file attributes.
http://www.techbytes...echbyte103.html

Credits to athlon32 for posting the link in an earlier thread here:
http://www.dreaminco...out-opening-it/

This post has been edited by dcomm: 19 July 2011 - 08:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: How to check if a file exists with out opening or creating a file

Posted 20 July 2011 - 01:57 AM

This is a system dependent problem. You can try to do it in a system independent way by attempting to open the file. Note that opening a file in write mode creates the file if it doesn't exist. Opening it in read mode doesn't. Use CreamDelight's solution for C, and this for C++:
//open file for output and create it if it doesn't exist
std::ofstream file(filename);
if(!file.is_open()) {
    //error: could not open/create file
}
//use file
file.close();

//open file for input and create it if it doesn't exist (not tested)
std::ifstream file(filename);
if(!file.is_open()) { //could not open file/file doesn't exist
    file.close();
    std::ofstream newFile(filename); //create file
    if(!newFile.is_open()) {
        //error: could not open/create file
    }
    newFile.close();
    file.open(filename); //reopen file
}
//use file
file.close();

This post has been edited by Hezekiah: 20 July 2011 - 04:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1