gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:16:42 27 4
gpt4 key购买 nike

这是我为创建 sic/xe .asm 文件的符号表而编写的代码....

 #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.'
    出现并且我的程序进入
    Debug模式...还有一个名为 iosfwd(std::char_traits<char>) 的文件出现一个 在线的箭头 _Left=_Right;以下功能:

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

  2. 此外,我在 block 的开始和结束时向控制台输出了一些单词 str[1]="start"检查此功能是否正常工作...虽然两行都是
    工作,我也确信输入正在被程序成功地从asm 文件(我已经检查过了),没有行输出到 intermfile 和 symtab...请帮助??

最佳答案

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

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

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

关于c++ - 文件处理(对于具有多个字段的文件)以及与之相关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7516868/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com