file processing (for file with more than one fields) and problems associated with it

淺唱寂寞╮ 提交于 2019-12-24 22:43:03

问题


here is a code i wrote to create the symtab of a sic/xe .asm file....

 #include<iostream>
 #include<fstream>
 #include<iomanip>
 #include"aviasm.h"
 using namespace std;

 void aviasm::crsymtab()
{

ofstream outs("symtab.txt",ios::out);//creating the symtab
ofstream outi("intermfile.txt",ios::out);//creating the intermediate file
ifstream in("asmfile.txt",ios::in);//opening the asmfile which i have already written
in.seekg(0,ios::beg);


char c;
string str[3];
string subset;
long locctr=0;
int i=0;


while((c=in.get())!=EOF)
{
    in.putback(c);
    while((c=in.get())!='\n')
    {
        in.putback(c); //putting back the first encountered letter
        in>>str[i++];  //the asm file has three or less fields in every row
    }

    if(str[0].size()!=0 && str[1].size()!=0 && str[2].size()!=0)//if all 3 are there
    {

        if(str[1]=="start")
        {
            outi<<hex<<locctr;
            outs<<str[1]<<" "<<locctr<<"\n";
            outs<<resetiosflags(ios::hex);
            outi<<" "<<str[0]<<" "<<str[1]<<" "<<str[2]<<"\n";
            locctr=stol(str[2],0,16);
        }//str[1]=start
     }//end of all the three fields
}
in.close();
outi.close();
outs.close();
}//end of crsymtab

.....here is a sample sic/xe .asm file.....note that in the above code i have not included the entire coded because the problem occurs even if i comment out the entire portion of the code except the above...the problem that occurs is whenever i run the code:

  1. A message box with: 'Unhandled exception at 0x00ba7046 in aviasm.exe: 0xC0000005: Access violation writing location 0xcccccccc.' appears and my program enters
    debugging mode...also a file named iosfwd(std::char_traits<char>) appears with an arrow at the line _Left=_Right; of the following function:

    static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right) { // assign an element _Left = _Right; }

  2. Also, I output a few words to the console at the start and end of the block str[1]="start" to check whether this function was working...although both lines were
    working and I am also sure that input is being successfully taken by the program from the asm file(I have checked this),no lines are being output to intermfile and symtab...plz help??


回答1:


You should run your program inside a debugger. If you are using Windows, then MSVC provides a debug environment. If you are using Linux, compile your program with -g, and run the gdb debugger: gdb ./myprog. You would immediately discover that on this line:

in>>str[i++];  //the asm file has three or less fields in every row

i has a value of 4, which exceeds the size of the str array.



来源:https://stackoverflow.com/questions/7516868/file-processing-for-file-with-more-than-one-fields-and-problems-associated-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!