Solved

Reading from a file

Posted on 1998-05-08
5
234 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now