简体   繁体   English

从文本文件读取特定条目

[英]Read Specific Entry From Text File

This is the text file named "TextFile.txt" 这是名为“ TextFile.txt”的文本文件。

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

And here is my c++ code: 这是我的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;
}

I want only the data. 我只想要数据。 So how can I skip the first line and read the second line immediately? 那么,如何跳过第一行并立即阅读第二行? If possible, how can I read only second column without wasting of time to read the first column? 如果可能的话,如何在不浪费时间阅读第一栏的情况下仅读取第二栏?

Use getLine() for reading first line and then start to read second line .. It the most possible choice I think. 使用getLine()读取第一行,然后开始读取第二行..我认为这是最可能的选择。

You can do such things ; 你可以做这样的事情;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM