473,513 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

determining failure cause of an fstream call

I need to open a file, and determine if:

1: I actually opened it
2: I couldn't open it because it doesn't exist, or
3: I couldn't open it for other reasons, such as file permissions.

I'm using ifstream for this, and I can tell if it opened or not. But if
it doesn't open, how do I tell whether the file exists or not?

Here's what I have so far:

code:

ifstream foo();
foo.open(filename);
if(foo.is_open()){
// It's open...read it.
}else if(BAR){ // file does not exist
// handle file does not exist case
}else{
// complain to user that you can't open the file
}

So I need to know what I can put in place of BAR.

Can I trust the value of errno in this case? Is BAR "(errno==ENOENT)"?

I need the same code working in VC++ and unix/g++, so I'm looking for a
platform-agnostic solution, if one exists.

--Rob
Jul 23 '05 #1
2 1932
"Rob Mandeville" <re*****@alum.wpi.edu> wrote in message
news:G6********************@adelphia.com...
I need to open a file, and determine if:

1: I actually opened it
2: I couldn't open it because it doesn't exist, or
3: I couldn't open it for other reasons, such as file permissions.

I'm using ifstream for this, and I can tell if it opened or not. But if
it doesn't open, how do I tell whether the file exists or not?


To my knowledge, standard C++ doesn't know about permissions. I am a total
newbie, though, so I could be wrong. Source:

http://www.cplusplus.com/ref/iostrea...xceptions.html

It doesn't seem to have anything except fail, which kind of makes sense,
considering permissions are not necessarily available on all platforms.

- James Aguilar
Jul 23 '05 #2

"Rob Mandeville" <re*****@alum.wpi.edu> wrote in message
news:G6********************@adelphia.com...
|I need to open a file, and determine if:
|
| 1: I actually opened it
| 2: I couldn't open it because it doesn't exist, or
| 3: I couldn't open it for other reasons, such as file permissions.
|
| I'm using ifstream for this, and I can tell if it opened or not. But if
| it doesn't open, how do I tell whether the file exists or not?
|
| Here's what I have so far:
|
| code:
|
| ifstream foo();
| foo.open(filename);
| if(foo.is_open()){
| // It's open...read it.
| }else if(BAR){ // file does not exist
| // handle file does not exist case
| }else{
| // complain to user that you can't open the file
| }

There is no portable way in *Standard C++* to know
for sure if a file exists or not - You will have to
resort to non-standard features for that.

Additionally, C++ has no notion of permissions, and
again you will need to look into the extensions
provided by your platform.

| So I need to know what I can put in place of BAR.

About the best you can do is:

else if( !foo )
// ...

It will indicate a failure to open the stream, but
it does not mean that the file does not exist.

| Can I trust the value of errno in this case? Is BAR "(errno==ENOENT)"?

If the documentation for your implementation says
so, then why don't you just try it ?

| I need the same code working in VC++ and unix/g++, so I'm looking for a
| platform-agnostic solution, if one exists.

I doubt you will get exactly what you need.

At best, you might want to use conditional compilation
with the pre-processor for implementation specifics.

Cheers.
Chris Val
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3122
by: x2164 | last post by:
hi all, Linux 2.4.28 Glibc 2.2.5 gcc 2.95.3 I'm new to Python. I've compiled Python 2.4 from tar file.
5
3111
by: Alexandros Frantzis | last post by:
Hello! Is there a standard way to find the reason a file stream open failed (eg file doesn't exist, access rights)? Thanks.
3
1614
by: Fred Nelson | last post by:
I'm devloping a Web Application in VB.NET. In my web.config file I have specified that untrapped errors are to be sent to the page "errorpage.aspx". This is working fine - if an untrapped error occurs the application is indeed routed to this page. On this page I would like to determine the cause of the error and either log it in a file or...
1
1166
by: daniel | last post by:
I'm devloping a Web Application in VB.NET. In my web.config file I have specified that untrapped errors are to be sent to the page "errorpage.aspx". This is working fine - if an untrapped error occurs the application is indeed routed to this page. On this page I would like to determine the cause of the error and either log it in a file or...
4
2148
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is broken in Firefox 1.5.0.1 on OS X. What happens with the Scriptaculous library is this In the html document the author only has to include one...
1
5336
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data doesn't show in the next read, or when I open the file itself. Additionally, I get a insufficient parameters error from if I remove the ios flags from...
6
17104
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
6
1953
by: canilao | last post by:
Hi All, So I ran into an issue with using the ! operator with fstream: // Make sure the file is ready to be read before we move on. 1: fstream fileStream; 2: fileStream.open("c:\\some_file.txt"); // Try to open until success. 3: while(!fileStream) fileStream.open("c:\\some_file.txt");
8
3113
by: =?Utf-8?B?TWFyaw==?= | last post by:
We've got a wierd failure happening on just one machine. One part of our product uses a 3rd party search implementation (dtSearch). DtSearch has a native core (dten600.dll), late-bound, and a managed wrapper (dtSearchNetApi2.dll). For reasons unknown our build and msi packaging process includes dtSearchNetApi2.dll but not dten600.dll in...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.