【问题标题】:C++ Reading a text file backwards from the end of each line up until a spaceC ++从每行的末尾向后读取文本文件直到一个空格
【发布时间】:2013-05-05 08:00:48
【问题描述】:

是否可以从每一行的末尾向后读取文本文件直到一个空格?我需要能够在每行末尾输出数字。我的文本文件格式如下:

1 | First Person | 123.45
2 | Second Person | 123.45
3 | Third Person | 123.45

所以我的输出是 370.35。

【问题讨论】:

标签: c++


【解决方案1】:

是的。但在您的情况下,简单地读取整个文件并解析出数字很可能更有效。

你可以做这样的事情(我用伪代码写这个,所以你必须写真正的代码,因为这是你学习的方式):

   seek to end of file. 
   pos = current position
   while(pos >= 0)
   {
       read a char from file. 
       if (char == space)
       {
          flag = false;
          process string to fetch out number and add to sum. 
       }
       else
       {
          add char to string
       }
       if (char == newline)
       {
           flag = true;
       }
       pos--
       seek to pos-2
   }

【讨论】:

    猜你喜欢
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多