C++ 文件处理(针对具有多个字段的文件)及其相关问题
下面是我编写的代码,用于创建sic/xe.asm文件的symtabC++ 文件处理(针对具有多个字段的文件)及其相关问题,c++,file-io,file-processing,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 o
#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
#包括
#包括
#包括
#包括“aviasm.h”
使用名称空间std;
void aviasm::crsymtab()
{
ofstreamouts(“symtab.txt”,ios::out);//创建symtab
ofstreamouti(“intermfile.txt”,ios::out);//创建中间文件
ifstream-in(“asmfile.txt”,ios::in);//打开我已经编写的asmfile
in.seekg(0,ios::beg);
字符c;
字符串str[3];
字符串子集;
长locctr=0;
int i=0;
而((c=in.get())!=EOF)
{
收回(c);
而((c=in.get())!='\n')
{
in.putback(c);//放回遇到的第一个字母
在>>str[i++];//asm文件每行有三个或更少的字段
}
如果(str[0].size()!=0&&str[1].size()!=0&&str[2].size()!=0)//如果所有3个都存在
{
如果(str[1]=“开始”)
{
outi您应该在调试器中运行程序。如果您使用的是Windows,则MSVC提供了一个调试环境。如果您使用的是Linux,请使用-g
编译您的程序,然后运行gdb调试器:gdb./myprog
。您将立即发现这一行:
in>>str[i++]; //the asm file has three or less fields in every row
i
的值为4,超过了str
数组的大小。“出现的问题是每当我运行代码时:”和“编译时抛出的异常”似乎不太匹配…@rob…我的最大值为3(不是4)…但我通过编写if解决了问题(i@rob...also为什么代码没有被输入到INTERM文件和symtab中?如果代码成功地从asmfile中取出,则数据没有从asmfile中成功输入。您的程序在从asmfile中提取数据时崩溃。因此,您永远无法到达输出到其他文件的行。@rob…抱歉,我误解了您的意思你的解决方案…你是正确的…我已经确定了问题…1me@rob....but这仍然不能解决为什么str的内容没有被输入到symtab和intermfile中的问题……因为我不能