从文件中获取输入
我有以下名为 asmfile.txt 的文件,
copy start 1000
read ldx zero
rd indev
rloop tix k100
为了从此文件中获取逐行输入,我编写了以下代码....
void aviasm::crsymtab()
{
ifstream in(asmfile,ios::in);//opening the asmfile
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);
str[0]="";
str[1]="";
str[2]="";
while((c=in.get())!='\n')
{
in.putback(c);
in>>str[i];
i==2?i=0:i++; //limiting i to 2....
}
cout<<str[0]<<" "<<str[1]<<" "<<str[2]<<endl;
}
in.close();
}
//现在的问题是前三行已成功输入到 str...但最后一行行没有被输入到 str...我知道这一点,因为在控制台上运行程序时我看到...
copy start 1000
read ldx zero
rd indev
“rd indev”缩进发生变化,因为 str[0]="rd" 和 str[1]="indev" .....请告诉我为什么第四行没有被输入到 str....
i have the following file named asmfile.txt
copy start 1000
read ldx zero
rd indev
rloop tix k100
in order to take linewise input from this file i wrote the following code....
void aviasm::crsymtab()
{
ifstream in(asmfile,ios::in);//opening the asmfile
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);
str[0]="";
str[1]="";
str[2]="";
while((c=in.get())!='\n')
{
in.putback(c);
in>>str[i];
i==2?i=0:i++; //limiting i to 2....
}
cout<<str[0]<<" "<<str[1]<<" "<<str[2]<<endl;
}
in.close();
}
//now the problem is that the first three lines are being successfully input into str...but the last line is not being input to str....i know this because when running the program on console i see...
copy start 1000
read ldx zero
rd indev
'rd indev' indentation changes because str[0]="rd" and str[1]="indev".....plz tell me why the fourth line is not being input into str....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
至于为什么你的代码失败:当asmfile的最后一行没有换行符时,
while((c=in.get())!='\n')
进入无限循环。将
i==2?i=0:i++;
更改为 i++ 并在 while 循环中移动int i=0;
,编辑 asmfile,使其具有换行符最后一行后面的字符,您的代码将起作用。也就是说,你确实应该按照雷内的建议去做。这样的代码很混乱并且容易出错。
As to why your code fails:
while((c=in.get())!='\n')
enters infinite loop when there's no new-line character in the last line of asmfile.Change
i==2?i=0:i++;
to i++ and moveint i=0;
in while loop, edit asmfile so it'll have a new-line character behind last line and your code will work.That said, you really SHOULD be doing it like Rene suggested. Code like this is messy and error prone.
我不知道逐字符读取文件并将它们放回流中的目的是什么。该行
定义了字符串
str[0]
到str[2]
。写入不存在的str[3]
是未定义的行为。更简洁的方法是Afterwardslines.size() 给出成功读取的行数。
I don't know what's the aim of reading the file character by character and putting them back into the stream. The line
defines strings
str[0]
tostr[2]
. Writing to non-existingstr[3]
is undefined behaviour. A cleaner approach would beAfterwards
lines.size()
gives the numbers of lines read successfully.我在调试您的代码时遇到问题,因为我不明白您的意图 -
get()
和pushback()
调用是怎么回事?。因此,我不直接回答您的问题,而是重写您的代码:I'm having trouble debugging your code, because I don't understand your intent -- what is the deal with the
get()
andpushback()
calls?. So, instead of answering your immediate question, let me rewrite your code: