从文件中获取输入

发布于 2024-12-06 07:22:00 字数 911 浏览 0 评论 0原文

我有以下名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋檐 2024-12-13 07:22:00

至于为什么你的代码失败:当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 move int 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.

顾铮苏瑾 2024-12-13 07:22:00

我不知道逐字符读取文件并将它们放回流中的目的是什么。该行

string str[3];

定义了字符串 str[0]str[2]。写入不存在的 str[3] 是未定义的行为。更简洁的方法是

std::ifstream in(asmfile);
std::vector<std::string> lines;
std::string line;

while (std::getline(in, line))
{
  lines.push_back(line);
}

Afterwardslines.size() 给出成功读取的行数。

for (size_t i = 0; i < lines.size(); ++i)
{
  std::cout << i << " :  " << lines[i] << '\n';
}

I don't know what's the aim of reading the file character by character and putting them back into the stream. The line

string str[3];

defines strings str[0] to str[2]. Writing to non-existing str[3]is undefined behaviour. A cleaner approach would be

std::ifstream in(asmfile);
std::vector<std::string> lines;
std::string line;

while (std::getline(in, line))
{
  lines.push_back(line);
}

Afterwards lines.size() gives the numbers of lines read successfully.

for (size_t i = 0; i < lines.size(); ++i)
{
  std::cout << i << " :  " << lines[i] << '\n';
}
ゝ杯具 2024-12-13 07:22:00

我在调试您的代码时遇到问题,因为我不明白您的意图 - get()pushback() 调用是怎么回事?。因此,我不直接回答您的问题,而是重写您的代码:

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>

const char * asmfile("/tmp/asm.txt");
void crsymtab()
{
  std::ifstream in(asmfile); // opening the asmfile

  std::string line;
  while(std::getline(in, line)) {
    std::istringstream sline(line);
    std::string str[3];
    if(sline.peek() == ' ')
      sline >> str[1] >> str[2];
    else
      sline >> str[0] >> str[1] >> str[2];
    std::cout << str[0] << " " << str[1] << " " << str[2] << "\n";
  }
}

int main() { crsymtab(); }

I'm having trouble debugging your code, because I don't understand your intent -- what is the deal with the get() and pushback() calls?. So, instead of answering your immediate question, let me rewrite your code:

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>

const char * asmfile("/tmp/asm.txt");
void crsymtab()
{
  std::ifstream in(asmfile); // opening the asmfile

  std::string line;
  while(std::getline(in, line)) {
    std::istringstream sline(line);
    std::string str[3];
    if(sline.peek() == ' ')
      sline >> str[1] >> str[2];
    else
      sline >> str[0] >> str[1] >> str[2];
    std::cout << str[0] << " " << str[1] << " " << str[2] << "\n";
  }
}

int main() { crsymtab(); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文