472,958 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Getting Error Text from ifstream

Hello group,

coming from C, I'm used to somthing like this when opening a file

const char *foobar = "this.txt";
FILE *f;
f = fopen(foobar, "r");
if (!f) {
fprintf(stderr, "Couldn't open %s: %s\n", foobar, strerror(errno));
exit(EXIT_FAILURE);
}

translating that somewhat to C++ yields me with

const char *foobar = "this.txt";
std::ifstream f(foobar);
if (!f) {
throw GenericException("Couldn't open file.");
}

So I can detect that opening the file wasn't successful, but I don't
know *why* (i.e. permissions, no such file or directory, etc.). My guess
is that strerror and errno still do what I expect them to do, however I
think that would be a very C-way to solve things.

How can I accomplish strerror(errno) on the std::ifstream with C++ means?

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
Nov 20 '07 #1
5 5385
On Tue, 20 Nov 2007 14:45:24 +0100 in comp.lang.c++, Johannes Bauer
<df***********@gmx.dewrote,
>So I can detect that opening the file wasn't successful, but I don't
know *why* (i.e. permissions, no such file or directory, etc.). My guess
is that strerror and errno still do what I expect them to do, however I
think that would be a very C-way to solve things.
Use strerror(), perror(), and errno and consider yourself lucky. The
C++ standard doesn't exactly promise that they will work for iostreams,
but they do for the compilers I use. That's the best you get for now.
Nov 20 '07 #2
David Harmon schrieb:
Use strerror(), perror(), and errno and consider yourself lucky. The
C++ standard doesn't exactly promise that they will work for iostreams,
but they do for the compilers I use. That's the best you get for now.
Oh, that's really a shame. Is anything planned about this in future
specifications?

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
Nov 20 '07 #3
On Tue, 20 Nov 2007 18:23:35 +0100 in comp.lang.c++, Johannes Bauer
<df***********@gmx.dewrote,
>David Harmon schrieb:
>Use strerror(), perror(), and errno and consider yourself lucky. The
C++ standard doesn't exactly promise that they will work for iostreams,
but they do for the compilers I use. That's the best you get for now.

Oh, that's really a shame. Is anything planned about this in future
specifications?
I have not heard of any. That would be a good question for
comp.std.c++, where the c++ standardization process is discussed.
Nov 20 '07 #4
On Nov 20, 2:45 pm, Johannes Bauer <dfnsonfsdu...@gmx.dewrote:
coming from C, I'm used to somthing like this when opening a file
const char *foobar = "this.txt";
FILE *f;
f = fopen(foobar, "r");
if (!f) {
fprintf(stderr, "Couldn't open %s: %s\n", foobar, strerror(errno));
exit(EXIT_FAILURE);
}
translating that somewhat to C++ yields me with
const char *foobar = "this.txt";
std::ifstream f(foobar);
if (!f) {
throw GenericException("Couldn't open file.");
}
So I can detect that opening the file wasn't successful, but I
don't know *why* (i.e. permissions, no such file or directory,
etc.). My guess is that strerror and errno still do what I
expect them to do, however I think that would be a very C-way
to solve things.
It's not really a C way either, since C doesn't say anything
about the state of errno after a failed fopen.

Pragmatically, it's what I do as well.
How can I accomplish strerror(errno) on the std::ifstream with
C++ means?
You can't associate errno with an std::ifstream anymore than you
can associate it a FILE*.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 20 '07 #5
James Kanze schrieb:
It's not really a C way either, since C doesn't say anything
about the state of errno after a failed fopen.

Pragmatically, it's what I do as well.
Does it really not? My Linux manpage says its fopen implementation is in
accordance with ANSI C3.159-1989 ("ANSI C") and errno (according to
another manpage) seems to appear in the ISO-C standard - yet the
connection of the two is purely a Linux thing and could break on any
other system?
>How can I accomplish strerror(errno) on the std::ifstream with
C++ means?

You can't associate errno with an std::ifstream anymore than you
can associate it a FILE*.
This is really a lack, as I think the language should provide means for
that. Many are, in fact, operating system independent after all.

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
Nov 20 '07 #6

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

Similar topics

5
by: Francis Bell | last post by:
I just found that my fin stream is not getting passed to my readInASpinnerbait function. Here's what I have: string readInFirstChars(ifstream &fin) { char first; string print; while...
7
by: mattsniderppl | last post by:
Hi, i'm relatively new to C++ from java and am having a difficult time with pointers. I'm sure there is something simple that I am doing wrong, but I can't seem to write this in a way that doesn't...
1
by: swtstrawberry | last post by:
This program is supposed to read in a text file with values of altitudes ranging from 0-100,000 increasing by 1000, with these altitude values there are corresponding pressure, temperature, and...
13
by: theronnightstar | last post by:
I seem to be having a problem with getline(). I have tried to find it on google, but I'm not seeing the answer. The goal of this section of code is simply to read in a line from a file to a...
29
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
21
by: blackx | last post by:
I got this error: error C2664: 'Vector::setVector' : cannot convert parameter 1 from 'const std::ifstream' to 'std::ifstream' How can it not be able to convert from std::ifstream to...
2
Schwack
by: Schwack | last post by:
I've just started learning C++ (got bored at work) and I'm using VC++ to compile some simple code but I get a compile error in test.cpp when using "getline". I've searched the internet and this...
2
by: Sejoro | last post by:
Hello, I am trying to write a program that opens a file; reads through it; outputs the text; then outputs the number of lines, words, and characters. Problem is, every time I try to compile, no...
10
by: charmeda103 | last post by:
My program keeps getting me and error and i dont why here is the error message error C2061: syntax error: identifier 'infile' error C2660: 'ReadDate' : function does not take 6 arguments...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.