?
Solved

Opening a file in an MFC app.

Posted on 1997-10-10
4
Medium Priority
?
728 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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

649 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