473,769 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_FAILU RE);
}

translating that somewhat to C++ yields me with

const char *foobar = "this.txt";
std::ifstream f(foobar);
if (!f) {
throw GenericExceptio n("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ästerli ch. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathemat ik <47************ **********@news .sunrise.ch>
Nov 20 '07 #1
5 5462
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ästerli ch. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathemat ik <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_FAILU RE);
}
translating that somewhat to C++ yields me with
const char *foobar = "this.txt";
std::ifstream f(foobar);
if (!f) {
throw GenericExceptio n("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 objektorientier ter Datenverarbeitu ng
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ästerli ch. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathemat ik <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
1899
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 (fin.good()) { fin.get(first, 4, '/');
7
4925
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 give me the error "Unable to write to memmory." This is a static function that receives a string that is the name of a file and an ifstream pointer. It then concatenates the path string using the string.h function strcat(char *, const char *) and...
1
1910
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 density values. The program also has to read in another text file with the following altitude values 8250,15360,34840,55685,72470,94522. The purpose of the program is to use the first text file to produce pressure, temperature, and density values for the...
13
2310
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 string according to the conditions of the do...while statement. /////code void populate( vector<string>& unplayed_anagrams, string& word, const
29
23219
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 write to the file: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) I don't know what I am doing wrong. I have posted my entire program
21
2562
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 std::ifstream??? Here's the relevant code (I didn't put all the variables as those are not relevant): header file: class Matrix{
2
5300
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 forum but can't find an answer. I hope that someone can help :-) test.h: #include <iostream> #include <fstream> #include <string> using namespace std; class ReadFile
2
5899
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 matter what modifications I make, I get an error, "line 42: Error: Could not find a match for std::basic_istream<char, std::char_traits<char>>::get(int)." I have tried everything I can think of. Help? #include <iostream> #include <fstream>
10
3070
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 error C2086: 'char &junkChar' : redefinition see declaration of 'junkChar'
0
9579
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3949
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
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.