Reading data from files

J

James Kanze

On 2007-04-08 19:08, David Harmon wrote:
That's C and would work if "inFile.open("people.dat");" had set errno,
but it does not.

It does, since it is defined in terms of fopen. How, of course,
or whether the error message is in any way appropriate to the
situation, is implementation defined. And of course, for
whatever reasons, the original poster apparently wanted the
error message on standard out, rather than standard error;
perror doesn't offer such a chose. (Most of my own work is on
servers, where both standard out and standard error are
connected to /dev/nul. So I'll generally output to a log file
or generate an email. I don't know why he assumes that everyone
would want to output to standard error.)

I also generally use a global class for such error messages,
which memorizes the fact that the error has occured, and
reflects it in the program return code.
 
J

James Kanze

I have another question. just now, I had read Ilia Yordanov's article
<All About: File I/O in C++>. (download from :http://www.cpp-home.com/
FileIO_tutorial.doc)
In his article,there're code:
#include <fstream>
using namespace std;
int main()
{
ofstream SaveFile("cpp-home.txt");
SaveFile << "Hello World, fromwww.cpp-home.comand Loobian!";
SaveFile.close();
return 0;
}
he said:"This program will create the file cpp-home.txt in the
directory from where you are executing it, and will put "Hello World,
fromwww.cpp-home.comand Loobian!" into it."

It might do that. If the user has autorization to create files
and write to the current directory. Not checking the results of
the open (in the constructor) is something no professional
programmer would do, however. It might, too, if the disk isn't
full, and there are no hardware errors. Not checking the
results of the close is something no professional programmer
would do, however.
why we could not open it as this: inFile.open("people.dat"); ???

Because you can't open an ofstream which is already open.

I generally prefer the separate open as well, because IMHO, it
makes it clearer what the following test is testing, but it's
really a question of style and personal taste, and in a code
review, I wouldn't even both with a comment if someone used the
implicit open in the constructor (unless, of course, the local
coding guidelines had a rule explicitly forbidding it).
I change it to "inFile.open("d:\\test\\people.dat");",only in this way
it can be right.

I'm not sure what context you're talking about. In the example
above, the code creates a file named "cpp-home.txt" in the local
directory, and writes to it. If he's lucky, and no error
occurs, of course.

In your example, you open a file using an absolute pathname, and
guessing from the name of your variable, you want to read it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top