【问题标题】:Read Specific Entry From Text File从文本文件中读取特定条目
【发布时间】:2013-09-04 14:21:50
【问题描述】:

这是名为“TextFile.txt”的文本文件

Stat    Rain
1       16
2       34
3       24
4       23
5       21
6       19
7       17
8       35
9       27

这是我的 C++ 代码:

#include<iostream>
#include<fstream>
using namespace std;
char Station[9];
char Rainfall[9];
int i;
int j;
int s[23];
double r[23];
main()
{
    ifstream Read;
    Read.open("TextFile.txt");
    Read>>Station;//I don't want this
    Read>>Rainfall;//I have no choice then I just assign these two variables
    i=0;
    while(!Read.eof())//This is the only I want
    {
        i++;
        Read>>s[i]>>r[i];
    }
    Read.close();
    for(j=1;j<i;j++)
    {
        cout<<s[j]<<"\t"<<r[j]<<endl;
    }
    return 0;
}

我只想要数据。那么如何跳过第一行并立即阅读第二行呢?如果可能,我怎样才能只阅读第二列而不浪费时间阅读第一列?

【问题讨论】:

  • stackoverflow.com/questions/13209650/… 可能对你有帮助!!
  • @baibhavk 嗯,这不是我的愿望。在我的文本文件中,前两个条目是标题。我想跳过它们,只读取数据。

标签: c++ text line fstream


【解决方案1】:

使用getLine() 阅读第一行,然后开始阅读第二行。我认为这是最可能的选择。

你可以做这样的事情;

ifstream stream("TextFile.txt");
string dummyLine;
getline( stream, dummyLine );
/*    
   Here you can read your values 
*/
while (stream)

【讨论】:

  • 你的意思是从第一行开始读吗?第一行是开头提到的标题。我想跳过它,只阅读前面标题下面的数据。
  • 我知道你只是用 getline() 阅读第一行,然后你可以阅读你的真实数据,也可以逐行阅读,不要使用 eof() 。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
相关资源
最近更新 更多