MmMm SsSs
MmMm SsSs

Reputation: 87

How to make c++ not create a new file if one doesn't exist?

I've been working with c++ and I noticed that whenever I use .open() for a file if there's no file with that name, it just creates one. Is there a way to have it not do this?

Upvotes: 0

Views: 460

Answers (1)

user18741504
user18741504

Reputation:

Check if the file exists first. The following link might help you:

Fastest way to check if a file exists using standard C++/C++11,14,17/C?

Or maybe, you could use the nocreate option:

void open(const char *filename, ios::openmode mode); where,

  • First argument *filename specifies the name of file and location.
  • Second argument open() member function defines the mode in which the file should be opened.

All the options here: https://www.tutorialride.com/cpp/file-handling-in-c.htm

Upvotes: 2

Related Questions