Solved

Reading from a file

Posted on 1998-05-08
5
236 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

775 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