indication about non existence of a file

indication about non existence of a file

Question:
Using ifstream, I want to open a file that already exists, and launch an error message if it does not exist. How can I get indication that the file that I’m trying to open does not exist?

Here is my code:

char* fileName = (char*)LPCTSTR(m_sFileName);	m_fFile.open(name, ios::in | ios::nocreate | ios::binary);	if(m_fFile.***)		return FALSE;	else 		return TRUE;

*** What should I put here in order to get a message that the file does not exist, and not create a new file?

Answer:
Before answering your specific question, a few stylistic issues. First, examine this statement:

char* fileName = (char*)LPCTSTR(m_sFileName);

Using C-style cast is better avoided in new code. Furthermore, in this case, the cast is totally uncalled for. Simply use a const char *. open() requires a const pointer anyway:

  const char* fileName = LPCTSTR(m_sFileName);

Secondly, don’t mix bool variables with TRUE and FALSE macros. Use the built-in ‘true’ and ‘false’ keywords instead:

  return false; //better style

This convention is safer, self-documenting, and will save you from serious maintenance problems in the future.

Now back to your question. You can call the fail() member function to examine the success or failure state of the last operation:

	if(m_fFile.fail()) /*returns 'true' on failure*/         return false;        else          return true;

As you can see this is verbose. A better way to code this statement is:

return !(m_fFile.fail())

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes