Solved

Opening a file in an MFC app.

Posted on 1997-10-10
4
706 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

756 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