从 file_2 获取输入
我正在从文件中逐行获取输入...每行包含三列或更少列... 如果文件中的行类似于
lda #5
或,
label +lda #5
则一切都很好,但是如果以“+”开头的行,
+lda #5
则未输入该行......为什么会这样?
输入获取代码是
ifstream in(asmfile,ios::in);
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]="";
i=0;
while((c=in.get())!='\n' && c!=EOF)
{
in.putback(c);
in>>str[i++];
}
}
i am taking input from a file row-wise...each row contains three or less columns...
everything is fine if the line in the file is like
lda #5
or
label +lda #5
but if a line starts with '+' like
+lda #5
that line is not being input....why is this so?
the input taking code is
ifstream in(asmfile,ios::in);
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]="";
i=0;
while((c=in.get())!='\n' && c!=EOF)
{
in.putback(c);
in>>str[i++];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你想用你的代码去哪里,但对我来说,它的构造似乎很糟糕,并且你将问题标记为 C++,所以我建议你使用标准 C++ 习惯用法来逐行读取文件:
I'm not sure where you were trying to go with your code, but it seems very poorly constructed to me, and you tagged the question as C++, so I would recommend you use the standard C++ idiom to read a file line by line:
正如所写的,我相信发布的代码将读取示例所示的行。事实上,我刚刚运行了它,它还读取了您指出不起作用的行(并且它只读取两个“单词”,因为在这种情况下
>>
运算符将使用白色 失败的一个可能原因是如果+lda #5
行前面的单词超过三个(由空格分隔),这将导致未定义的行为(很可能是访问冲突)因为
str
数组只有三个元素,并且不会检查超过三个单词的情况。As written, I believe the posted code will read the lines shown as examples. In fact, I just now ran it, and it also read the line that you indicated would not work (and it reads just two "words" since the
>>
operator in that case will use white space as a delimiter for the string.A possible reason for the failure is if the line preceding the
+lda #5
line has more than three words (delimited by spaces). That would result in undefined behavior (quite possibly an access violation) since thestr
array has only three elements, and there is no check for the case where more than three words are on the line.