Read Specific Entry From Text File

我是研究僧i 提交于 2019-12-11 19:42:31

问题


This is the text file named "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:

#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?


回答1:


Use getLine() for reading first line and then start to read second line .. It the most possible choice I think.

You can do such things ;

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


来源:https://stackoverflow.com/questions/18616432/read-specific-entry-from-text-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!