Solved

Opening a file in an MFC app.

Posted on 1997-10-10
4
690 Views
Last Modified: 2006-11-17
Recently, a couple of us wrote some data acquisition software for our school's physics department.  The device takes linear displacement vs time data.  The problem is when we try to open a previously saved it, we get an illegal operation error.  We're using Visual C++ 4.0 and this is an MFC project.  Our OnOpenFile function uses the ifstream command.  

Source Code looks like:
BOOL CSonicBetaDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
      if (!COleServerDoc::OnOpenDocument(lpszPathName))
            return FALSE;
      
      ifstream ar( lpszPathName, ios::in, filebuf::sh_read||filebuf::sh_write );
      char Dummy[50];

      ar >> m_numpoints;

      ar >> Dummy;
      ar >> Dummy;
      ar >> Dummy;
      ar >> Dummy;

      ar >> Dummy;
      ar >> Dummy;
      ar >> Dummy;
      ar >> Dummy;

      for( int i=0; i<m_numpoints; i++ )
      {
            
            ar >> m_time[i];
            ar >> m_DataPoint[i];

The OnSaveFile() command looks like the OnOpenFile except it uses ofstream instead of ifsream.  

Error looks like:
SONIC BETA caused an invalid page fault in
module KERNEL32.DLL at 0137:bff86181.
Registers:
EAX=00000000 CS=0137 EIP=bff86181 EFLGS=00010202
EBX=81582120 SS=013f ESP=00560000 EBP=005600a8
ECX=81582120 DS=013f ESI=81581d04 FS=4527
EDX=0000000e ES=013f EDI=005600c0 GS=0000
Bytes at CS:EIP:
53 56 e8 ea d9 00 00 8d b8 98 00 00 00 8b e8 8b
Stack dump:
00000001 005600a8 005600c0 005600dc 0065f834 00000000 bff858f8 00000001 0000000e 005600c0 005600dc 00560288 005602a4 0065f834 00000000 00000000

Let me know if you need any more details.  Thanks.

      
0
Comment
Question by:Two101097
4 Comments
 
LVL 2

Expert Comment

by:anichini
ID: 1171094
You need to give more context.

1) Where is it crashing? (file, line)
2) What does the stack trace look like?
3) a small snippet of source code demonstrating the problem would be nice.
0
 

Author Comment

by:Two101097
ID: 1171095
Edited text of question
0
 
LVL 5

Expert Comment

by:yonat
ID: 1171096
Could be:
1. Dummy is too short for one of the lines.
2. m_numpoints is too large for the actual arrays m_time or
   m_DataPoint.
3. m_time or m_DataPoint are not allocated.

Where exactly does the crash happen? Try tracing the exact line of the creash.

0
 
LVL 3

Accepted Solution

by:
gaohong earned 200 total points
ID: 1171097
Page fault could be caused by many factors, by just looking the code, assuming it is used in the project. There are several places deserve better attention. Do a search on //XXXX, and try to answer my questions followed, after implementing my suggestions, you will fix the problem.

good luck
gaohong


BOOL CSonicBetaDoc::OnOpenDocument(LPCTSTR
                 lpszPathName)
                 {
//XXXX   add:  if(!lpszPathName || *lpszPathName == 0) return false; better ones, further check it the file exist
 
             if (!COleServerDoc::OnOpenDocument(lpszPathName))
                 return FALSE;

                 ifstream ar( lpszPathName, ios::in, filebuf::sh_read||filebuf::sh_write );
                 char Dummy[50];
     //XXXX   I would increase the Dummy length to at least 133.

                 ar >> m_numpoints;
     //I would add string sentinel in the file. Upon reading,
     //search for the sentinel. In this way, you know it is
     //your application's file signature.
     //I would read firstly into Dummy string, assuming text file
     //then check the string for possible intepretation of the      //line, only if it is in m_numpoints's legal range, then
     //assign m_numpoints.
 
                 ar >> Dummy;
                 ar >> Dummy;
                 ar >> Dummy;
                 ar >> Dummy;

                 ar >> Dummy;
                 ar >> Dummy;
                 ar >> Dummy;
                 ar >> Dummy;
//XXXX   make sure here, your m_DataPoint[] and m_time[] length
//are not smaller than m_numpoints
                 for( int i=0; i<m_numpoints; i++ )
                 {

                 ar >> m_time[i];
                 ar >> m_DataPoint[i];
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

932 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

9 Experts available now in Live!

Get 1:1 Help Now