文件处理(对于具有多个字段的文件)以及与之相关的问题

时间:2011-09-22 14:54:03

标签: c++ file-io file-processing

这是我写的一个代码,用于创建sic / xe .asm文件的symtab ....

 #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

.....这里有一个示例sic/xe .asm file .....请注意,在上面的代码中我没有包含整个编码,因为即使我注释掉代码的整个部分也会出现问题除了以上...发生的问题是每当我运行代码时:

  1. 出现一个消息框:'Unhandled exception at 0x00ba7046 in aviasm.exe: 0xC0000005: Access violation writing location 0xcccccccc.',我的程序输入
     调试模式...还出现一个名为iosfwd(std::char_traits<char>)的文件  箭头在线  _Left=_Right;以下函数:

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

  2. 此外,我在块的开头和结尾输出几个字到控制台 str[1]="start"检查此功能是否有效......尽管这两行都是正常的 工作,我也确信程序成功地输入了 asm文件(我已经检查了这个),没有行输出到intermfile和symtab ... plz help ??

1 个答案:

答案 0 :(得分:1)

您应该在调试器中运行程序。如果您使用的是Windows,则MSVC会提供调试环境。如果您使用的是Linux,请使用-g编译程序,然后运行gdb调试器:gdb ./myprog。你会立刻发现这一行:

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

i的值为4,超出了str数组的大小。