Hey guys I am using code that does the following:

cin.ignore(); 
cin.getline(file, 250);

ifstream test;

  test.open (file);

	if (test.fail())
		{
		  cout<<"error"<<endl;

		  exit(1);
	     }

...

The problem is that if a file name is entered that does not exist for example c:\\fred.txt the error message will not appear and the txt file fread will be created, but if I enter "c:\\fred.txt" the code seems to work, but I have created similar code before using cin.getline and the quotes were not needed. Any ideas what I have done wrong?

Thanks.

Dani AI

Generated

Brief diagnosis referencing the thread: observed that a supposedly read-only open was creating a new .txt file unless the input was typed a particular way; correctly suggested not to type C-style escapes at the console and to check the stream with is_open() (or the stream boolean). The fact that ios::nocreate “fixed” the symptom points to either the stream being opened in a write mode somewhere, or the filename read from the console being different from what was expected.

Core facts to keep in mind: a C++ input file stream opens in read mode by default and will set the failbit rather than create a file when the name does not exist; an output stream opens in write mode and will create the file if it does not exist. Use is_open() or the stream boolean to test whether an open succeeded. (docs.cppreference.com)

Practical checks and fixes (apply in order):

  • Observe exactly what was read from the console (show delimiters) to catch stray quotes, CR/LF or invisible characters: print ['filename'] or the character codes.

  • Prefer std::string + std::getline(std::cin, path) over cin.getline(char*,...) to avoid buffer/truncation issues; strip surrounding quotes and trailing \r if present.

  • Verify existence with a read-only open or std::filesystem::exists(path) before ever opening for write. Example pattern:

    std::string path;
    std::getline(std::cin, path);
    // strip surrounding quotes if any, then:
    std::ifstream in(path);
    if (!in) { /* missing or wrong name */ }

    std::getline and <filesystem> checks are the portable ways to read and validate names. (cppreference.com)

About ios::nocreate: that flag is a legacy, non‑standard relic from older headers and libraries. It can hide the real cause (wrong open mode or malformed filename) and is not portable; the robust pattern is to open for reading first (or use std::filesystem::exists) and only open for writing when the desired semantics are explicit.

Summary tie-back: if test.is_open() returned false even though a file “exists,” the filename read is the likely culprit (extra quotes, backslashes, CR, or accidental leading/trailing whitespace) or the code is opening in an output mode somewhere — print the read string, switch to std::getline, trim it, and the behavior will become deterministic.

Recommended Answers

All 5 Replies

when you enter the filename from the keyboard do not use double backslash -- double slashes are only needed for string literals in your program. So you should type c:\fred.txt , only one backslash. Reason: the compiler does not interpret anything you enter from the keyboard, it only interprets string literals.

Hi thanks for your reply but when i do this I still have the same problem.

then the problem is elsewhere. try test.is_open() instead of if test.fail(). Use your compiler's debugger and look at what exactly is in the file buffer, or use cout to display it.

then the problem is elsewhere. try test.is_open() instead of if test.fail(). Use your compiler's debugger and look at what exactly is in the file buffer, or use cout to display it.

Hi thanks again for your reply, when i use test.is_open() it doesn't find any file even if it exists.

I have managed to solve the problem by using ios::nocreate, but as I used ifstream I thought I shouldn't have to use this, any ideas? Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.