Solved

Program crashing

Posted on 1998-09-02
12
243 Views
Last Modified: 2008-02-01
I have a small program which does some strings extractions from a text file. The problem I have is that the compiler does not detect any error, but when I run the program it crashes with a fatal error. Here is the code which seems to be creating the problem:

int      const maxsize = 256;
char      LineIn[maxsize];
f.ReadString(LineIn,maxsize -1);

After doing a step by step testing, it seems that it is the last line which is creating the problem.

Below is the error message I get:

TADS caused an invalid page fault in
module TADS.EXE at 014f:0041e239.
Registers:
EAX=37202020 CS=014f EIP=0041e239 EFLGS=00010216
EBX=00000000 SS=0157 ESP=0072eff4 EBP=0072f240
ECX=0072f04c DS=0157 ESI=0072f04c FS=2477
EDX=00a71080 ES=0157 EDI=00000004 GS=0000
Bytes at CS:EIP:
83 78 f4 01 7f 05 3b 78 fc 7e 0f 8b ce e8 43 ff
Stack dump:
00000004 0072f04c 0041e332 00000004 0072f058 0072f04c 0041e3c0 00000004 0072f058 0072f228 0072f7a0 00402063 0072f058 00008298 0072f274 0042d14c

Can anyone tell me why it is crashing and how I should change my code if necessary ?

Thanks,
Arnaud.
0
Comment
Question by:decornez
  • 3
  • 2
  • 2
  • +5
12 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1171924
The only ReadString I know of is in CArchive (MFC)

presumably f is _properly_constructed_ and _open_ one of these ?
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171925
I know a little MFC, the ReadString is part of CArchive, CStdioFile, and CInternetFile.

f has been opened like:
f.Open (Filename, Flags);
right?

Please post more of your code so more people can help.  Thanks.
0
 
LVL 1

Expert Comment

by:Andy_Keys
ID: 1171926
are you using UNICODE?!!
0
 
LVL 3

Expert Comment

by:elfie
ID: 1171927
Is it the first time that read the last line?
0
 

Expert Comment

by:aschi
ID: 1171928
Try:

TCHAR LineIn[maxsize];

wbr
Aschi
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171929
Is that an answer or a guess?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:Answers2000
ID: 1171930
You're right thresh on where ReadString is,

should also be in CGopherFile and CHttpFile which derive of CInternetFile (I haven't checked if it's been re-access specified as private - but MFC doesn't usually do that).

Anyway more info would help with a solution.
0
 

Author Comment

by:decornez
ID: 1171931
aschi,

I have tried TCHAR and it does not make any difference. So in order to be more precise, here is the listing of my code. Please note that the index number in my arrays is set at 1 only as I am just testing. Please ignore some of the comments in there which pertain to previously erased lines of code. So, Here we go (There is quite a lot):

      CDialog::OnInitDialog();
      
      // TODO: Add extra initialization here
      //##S

      //Open the file C:\Tads\DPRM files\MDRYieldCurvesCcy.HKD.01
      CStdioFile      f;//or should it only be CFile ??
      if (f.Open("C:\\Tads\\DPRM files\\MDRYieldCurvesCcy.HKD.01",CFile::modeRead)==FALSE)
            return TRUE;

      //Create an archive object
      CArchive ar( &f, CArchive::load);

      //Read data from the file
      char      s1[10];
      LONG      l1 = 104;
      int      p1 = f.Seek( l1, CFile::begin );
      //LONG lOffset = 104, lActual;
      //lActual = f.Seek( lOffset, CFile::begin );
      int bytesread1 = f.Read(s1,3);
      s1[bytesread1]=0;
      m_CcyEdit = s1;
      

      char      s2[10];
      LONG l2 = 301;
      int p2 = f.Seek( l2, CFile::begin );
      int bytesread2 = f.Read(s2,3);
      s2[bytesread2]=0;
      m_STDayCount = s2;

      char      s3[10];
      LONG l3 = 305;
      int p3 = f.Seek( l3, CFile::begin );
      int bytesread3 = f.Read(s3,3);
      s3[bytesread3]=0;
      m_STBase = atof(s3);


      m_FutDayCount = "";
      m_FutBase = "";

      char      s4[10];
      LONG l4 = 325;
      int p4 = f.Seek( l4, CFile::begin );
      int bytesread4 = f.Read(s4,3);
      s4[bytesread4]=0;
      m_LTDayCount = s4;

      char      s5[10];
      LONG l5 = 329;
      int p5 = f.Seek( l5, CFile::begin );
      int bytesread5 = f.Read(s5,3);
      s5[bytesread5]=0;
      m_LTBase = atof(s5);

      char      s6[10];
      LONG l6 = 358;
      int p6 = f.Seek( l6, CFile::begin );
      int bytesread6 = f.Read(s6,9);
      s6[bytesread6]=0;
      m_FXRate = atof(s6);


      char      s7[20];
      LONG l7 = 257;
      int p7 = f.Seek( l7, CFile::begin );
      int bytesread7 = f.Read(s7,9);
      s7[bytesread7]=0;
      m_ValDate = s7;

      //***********************************************
      //***********************************************
      //We now extract the data where which are affected by the
      //variable line length problem. Meaning that depending on the
      //number of digits that the number to the right of the "-" sign has,
      //then the line will have a different total length. Therefore we need
      //to find the position of the end of the line before we can copy
      //the necessary number of characters the right of the "-" sign
      //and the end of the line.

      //We define the different breakpoints and area sizes to be read
      int      addontenor1 = 0;
      int      addoninstru1 = 3;
      int      addonbid1 = 9;
      int      sizetenor = 3;
      int      sizeinstru1 = 4;
      int      sizebid1 = 10;

      
//      //We extract the line to calculate the various positions
//      int      const maxsize = 256;//256 is the maximum size of a line
//      char      LineIn[maxsize];
//      f.ReadString(LineIn,maxsize);
//
//      int      eoladdon = strcspn(LineIn,"\n");
//      int      anchorchar = strcspn(LineIn,"-");
//      m_HkdBid8 = eoladdon;
//      m_HkdAsk8 = anchorchar;

      
      //Start of the loop using the for statement
      int            i = 1;
      int      const      arraysize = 1;
            //for      (i = 0; i <= arraysize; i++) {//opening of the for loop

      //We take position 370 as the anchor point to start with in the file
      //and we start to work our way down from there
      int linestart1 = 370;
      int linepos1 = f.Seek( linestart1, CFile::begin );
      //We define the arrays where we are going to store
      //what we read from the file

      CString      tenor[arraysize];
      CString      instru[arraysize];
      double      bid[arraysize];
      double      ask[arraysize];

      int            tenorread[arraysize];
      int            instruread[arraysize];
      int            bidread[arraysize];
      int            askread[arraysize];

      char      tenorbuff[10];
      char      instrubuff[10];
      char      bidbuff[15];
      char      askbuff[15];


      //We now read the various data we are looking for
      //using our linestart reference. We start by reading the tenor.
      tenorread[i] = f.Read(tenorbuff,sizetenor);//Here we read the tenor for line 1
      tenorbuff[tenorread[i]]=0;
      tenor[i] = tenorbuff;
      m_HkdTenor1 = tenor[i];
      
      //Here we read the Instrument
      linepos1 = f.Seek( linestart1 + addoninstru1, CFile::begin );
      instruread[i] = f.Read(instrubuff,sizeinstru1);
      instrubuff[instruread[i]]=0;
      instru[i] = instrubuff;
      m_HkdInstru1 = instru[i];

      //Here we read the Bid side
      linepos1 = f.Seek( linestart1 + addonbid1, CFile::begin);
      bidread[i] = f.Read(bidbuff,sizebid1);
      bidbuff[bidread[i]]=0;
      bid[i] = atof(bidbuff);
      m_HkdBid1 = bid[i];

      //Here we read the Offer side
      linepos1 = f.Seek( linestart1, CFile::begin);
      //We extract the line to calculate the various positions
      int      const maxsize = 256;//256 is the maximum size of a line
      TCHAR      LineIn[maxsize];
      f.ReadString(LineIn,maxsize -1);      //################################
      //char      linebuff[256];
      //int            lineread[arraysize];
      //lineread[i] = f.Read(linebuff, 10);
      //linebuff[lineread[i]]=0;


//      int      eoladdon = strcspn(LineIn,"\n");
//      int      anchorchar = strcspn(LineIn,"-");
//      linepos1 = f.Seek( linestart1 + anchorchar + 2, CFile::begin);
//      askread[i] = f.Read(askbuff,(eoladdon - anchorchar - 2));
//      ask[i] = atof(askbuff);
//      m_HkdAsk1 = ask[i];




      //Close the archive
      ar.Close();

      //Close the file
      f.Close();

      //Update the screen with the new values of m_CcyEdit
      UpdateData(FALSE);

      //}//close of the for loop

      //##E

      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}

            //}//close of the for loop



0
 
LVL 1

Expert Comment

by:mlaiosa
ID: 1171932
Try reduceing maxsize to 255.   256 is a 9 bit number.
0
 
LVL 2

Accepted Solution

by:
kinkajou earned 30 total points
ID: 1171933
Use a CString for LineIn instead of a char.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171934
kinkajou - That is MFC.  I do not think decornez is using MFC.
0
 

Author Comment

by:decornez
ID: 1171935
Sorry guys,

I am moving to a different country (Hong Kong)and my PC has been packed. So I will not be able to try your proposed answers/comments probably for another 2 to 3 weeks. I will revert to you as soon as I am back online at home (I am using my office PC to post this message).

Thanks,
Arnaud.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

22 Experts available now in Live!

Get 1:1 Help Now