Link to home
Start Free TrialLog in
Avatar of decornez
decornez

asked on

Program crashing

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.
Avatar of Answers2000
Answers2000

The only ReadString I know of is in CArchive (MFC)

presumably f is _properly_constructed_ and _open_ one of these ?
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.
are you using UNICODE?!!
Is it the first time that read the last line?
Try:

TCHAR LineIn[maxsize];

wbr
Aschi
Is that an answer or a guess?
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.
Avatar of decornez

ASKER

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



Try reduceing maxsize to 255.   256 is a 9 bit number.
ASKER CERTIFIED SOLUTION
Avatar of kinkajou
kinkajou

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
kinkajou - That is MFC.  I do not think decornez is using MFC.
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.