?
Solved

Opening a file in an MFC app.

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
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 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…
Suggested Courses

752 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