Advertisement

fstream.is_open, memory mapping and a macro questions

Started by July 05, 2000 08:17 PM
4 comments, last by gimp 24 years, 5 months ago
Ok both questions are rolled in to one. I want to check to see if my fstream open was sucessful. I''ve been looking through MSDN woeful exampleless doco on this and havent come up much besides the following:
    
#define FAILURE			((Boolean)0)

	if (FAILURE(f.is_open))
	{
		cout << "Error failed to open file : " << Filename.c_str();
	}
	Data.push_back(f.get(c));

    
First I''m attempting to define a macro to test for success. The idea is that 0 is always faluse and anything else is true. (Boolean is just a typedef''d int). This doesn''t work, I get the feeling I should be doing this with a template but I''m not sure how to write it so it a lightweight as a macro(inline?) Next I''m trying to determine if there was a problem opening the file. Ijn general I''m used to being able to get back a whole range of errors reporting the reason why a file failed to open, perhaps it''s read only and I''m opening in write... is_open seems insufficient but I can''t find anything else. Next, I''m attempting to drop the data in to a vector(Data). Previously, I was using C to seek\getpos to determin the size of the file, allocing the ram and reading the whole thing in at once. Anyone know how to make that work in C++ (with a vector?) Many thanks once again guys.... Chris ''gimp'' Brodie
Chris Brodie
Well, that's not really a macro... You're just defining that it's gonna replace anywhere you write 'FAILURE' with '((Boolean)0)' and that doesn't do anything. Basically, that always evaluates to false, because you're doing nothing more than substituting in 0. Macros have to take parameters. You probably want something like:

#define FAILURE(x) (!x)

or

#define FAILURE(x) (x == 0)

or something like that. Both of those will evaluate to an integer, either 1 for true or 0 for false.

Hope I helped...

Edited by - Qoy on July 6, 2000 1:49:16 AM
Advertisement
Why not simply use:
    if ( ! file.open() ){  // ...}    

IMHO, it''s more natural than the Windows approach of defining macros to get result codes.

Erik
good() = what to call if you want to see if it succeeded.
bad() = what to call if problems are a possibility.
fail() = what to call if problems are fatal. // Bad also included in this call.//

I believe all file streams have these built in. Call clear() if problem is non-fatal and you can recover/go on etc...

YAP YFIO,
deadlinegrunt

Edited by - deadlinegrunt on July 6, 2000 10:06:44 PM

~deadlinegrunt

Ok, thanks guys...

1) In Fstream''s how do I get an error message? Like ERR_READONLY etc
2) Whats the best way of reading in the whole file at once? I used to use flen to get the langth and fread (blah, size, blah) to tell C to just read in the all the bytes in the file...

thanks again...
Chris Brodie
    //main.cpp#include <iostream>#include <fstream>#include <strstream>#include <string>using namespace std;int main(int argv, char** argc ){	ifstream	inFile( "main.cpp" );	strstream	buffer;	string		display;	if( inFile.good() ) //  If it's all good	{		buffer << inFile.rdbuf(); //  Move some bits to a memory stream		display = buffer.str(); //  Transfer it to something we can see for this example		display.resize( buffer.tellp() ); //  Just resizing to save space etc...		cout << display << endl; //  For demonstration purposes, show this source	}	return 0;}    

Not that this is the only way to flip bits, hopefully it gives you a playground to get sand in your shoes...

YAP YFIO,
deadlinegrunt

Forgot the neat little source box

Edited by - deadlinegrunt on July 6, 2000 10:40:42 PM

~deadlinegrunt

This topic is closed to new replies.

Advertisement