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

标签 c++ file-io file-processing

这是我为创建 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/

相关文章:

c++ - 使用 OpenMP 的复杂类型

C++:如何将unicode字符打印到文本文件

python - 从文件中检索文本 block

java - 如何使用Java nio在写操作期间检测磁盘已满?

C++:文件处理:匹配顺序文件

c++ - 使用try-catch C++处理文件

c++ - 如何添加易于在需要时打开和关闭而不产生生产开销的日志?

c++ - 在 C++ 中使用指向成员函数的函数指针数组

c# - 串行,RS422,在C#中,TxDone事件未触发,未接收到任何数据

java.nio.file.NoSuchFileException : why nio not creating file