Solved

Opening a file in an MFC app.

Posted on 1997-10-10
4
682 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
Comment Utility
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
Comment Utility
Edited text of question
0
 
LVL 5

Expert Comment

by:yonat
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
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.

763 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

13 Experts available now in Live!

Get 1:1 Help Now