Solved

Reading from a file

Posted on 1998-05-08
5
238 Views
Last Modified: 2012-06-27
I am doing some DirectX programming with Visual C++ 5.0.  I am currently trying to load in a bitmap file, and have a reference book with an example.  The code they use for opening a file and reading from it is as follows:

HFILE hfile = _lopen(filename, OF_READ);

if( _lread( hfile, &m_bmFileHeader, sizeof(m_bmFileHeader)) == sizeof(m_bmFileHeader))
{
     other code is here
}

The m_bmFileHeader is a structure of type BITMAPFILEHEADER which contains the following members:

WORD      bfType;
DWORD   bfStyle;
WORD      bfReserved1;
WORD      bfReserved2;
DWORD   bf0ffBits;

Now, I know that _lopen and _lread are older 16-bit commands, and would like to replace them with a newer 32 bit command.  I have looked at the fstream commands and also the CreateFile and ReadFile commands in VC++, but found that they do not work the same (and CreateFile seems like extreme overkill).  What commands can I use as the equivalent.  I really need to read in a block of bytes into this structure like they do in the _lread command above.  Please help!

Thanks a lot... let me know if this question is harder than it seems...
0
Comment
Question by:visual032798
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1174909
CreateFile and ReadFile are the 32 but counterparts.  You should use them OR the standard C++ runtime library procedures (fstream stuff).  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174910
If you don't like the functions, you can write your own simpler functions that use these funnctions.  Then you can use your simpler functions.  For example you could write a function that returns false to indicate a problem when ReadFile() was not able to read the reqeusted length (meaning you hit the end of the file.).

I can help you with these functions if you have more specific questions.  At the moment your question is should I use them?  and the answer is yes.
0
 

Author Comment

by:visual032798
ID: 1174911
I notice with the _lopen command, they are able to read directly into that BITMAPFILEHEADER structure, but with ReadFile, it said I couldn't do that because it couldn't translate it to char*.  Am I doing something wrong?  How do you recommend using the CreateFile and ReadFile commands (could you show me an example) on how to do this?  Thanks again.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1174912
The following sample is an "interface" for the ReadFile() function.  It returns one of two values.  One indicates no error.  The second indicates the end of the file was reached during the read.  Any other error is handled by displaying an error message and invoking the debugger.  

The nice thing about this function is you don't have to pass a pointer to a varaible to receive the read length.  ReadFile requires this variable, but it is inconvenient.  This function passes one for you so you don't have to.  It also examines the value returned in the variable and determines how to handle it.

//      This procedure reads the data of a specified length from a file specified by a file       //
// object.  The data is read starting at the current file pointer position.  It is an error to    //
// specify a file object that is not open.  This procedure is similar to the Red() procedure,     //
// except, the read procedure logs an error when the end of file is reached.                      //
//                                                                                                //
//      If the file is not open or any other read error occurs (except for reaching the end of    //
// the file), a terminal error will be logged.                                                    //
//                                                                                                //
// +--------------------------------------------------------------------------------------------+ //
// ¦Error Codes                                                                                 ¦ //
// +--------------------------------------------------------------------------------------------¦ //
// ¦Error Code¦Description                                                                      ¦ //
// +----------+---------------------------------------------------------------------------------¦ //
// ¦QFilErrNon¦No error occurred.                                                               ¦ //
// ¦QFilErrEnd¦Attempt to read past end of file occured.  Only portion up to end of file will   ¦ //
// ¦          ¦have been read.                                                                  ¦ //
// +--------------------------------------------------------------------------------------------+ //
int                                              // Error code.                                   //
QFil::FilRedAvl(QMemPtr  DatPtr,                 // -> buffer to receive data that is read.       //
                                                 // Buffer must be long enough to store the       //
                                                 // specified data.                               //
                QFilOff  RedLen,                 // Length to be read.                            //
                QFilOff &LenRed) const           // Returns length read from the file.            //
{
   QFilChk(*this,true,false);                    // Check this file object.                       //

   int ErrCod = QFilErrNon;                      // Error code to be returned.                    //

   if (!ReadFile(FilHnd,DatPtr,RedLen,&LenRed,NULL)) // If file can't be read from, then          //
      WndErr(__LINE__,"File",true);              // Display an error.                             //
   else if (RedLen != LenRed)                    // Otherwise if requested length wasn't read,    //
      ErrCod = QFilErrEnd;                       // Indicate that end of file was reached.        //

   return ErrCod;                                // Return the error code.                        //
}



If you still have code that the compiler can't compile, post it for me to see.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174913
Opps.  I'm wrong.  I didn't read my own code carefully.  For that function you DO need to pass a variable to receive the length read.

But I have a second function, built on the first one, where you don't have to pass a varaible to receive the length read.  The code is below  This way I can choose a function that suits my needs.  One is for the case when I want to know exacly how much is read.  The other is for the case when I require that the entire length be read.

//      This procedure reads the data of a specified length from a file specified by a file       //
// object.  The data is read starting at the current file pointer position.  It is an error to    //
// specify a file object that is not open.  This procedure is similar to the Red() procedure,     //
// except, the read procedure logs an error when the end of file is reached.                      //
//                                                                                                //
//      If the file is not open or any other read error occurs (except for reaching the end of    //
// the file), a terminal error will be logged.                                                    //
//                                                                                                //
// +--------------------------------------------------------------------------------------------+ //
// ¦Error Codes                                                                                 ¦ //
// +--------------------------------------------------------------------------------------------¦ //
// ¦Error Code¦Description                                                                      ¦ //
// +----------+---------------------------------------------------------------------------------¦ //
// ¦QFilErrNon¦No error occurred.                                                               ¦ //
// ¦QFilErrEnd¦Attempt to read past end of file occured.  Only portion up to end of file will   ¦ //
// ¦          ¦have been read.                                                                  ¦ //
// +--------------------------------------------------------------------------------------------+ //
int                                              // Error code.                                   //
QFil::FilRedAvl(QMemPtr DatPtr,                  // -> buffer to receive data that is read.       //
                                                 // Buffer must be long enough to store the       //
                                                 // specified data.                               //
                QFilOff RedLen) const            // Length to be read.                            //
{
   QFilOff LenRed;                               // Length read from the file.                    //

   return FilRedAvl(DatPtr,RedLen,LenRed);       // Try to read from the file.                    //
}

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question