使用getline读取文件时跳过行 [英] skip lines while reading file with getline

查看:86
本文介绍了使用getline读取文件时跳过行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在阅读.csv文件时跳过行。使用getline?



现在它将所有行存储到数组中。(将第二列存入数组)

How to skip lines while reading .csv file. using getline?

Right now it is storing all lines into array.(storing 2nd column into array)

array A[]= {0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.06369,0.06369,0.06369,0.06369,0.06369,
0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.03333,0.03333  }



它应该存储1,5,10,15,20,25行数组。



所需输出:


It should store 1,5,10,15,20,25 lines into array.

Required output:

array A[]= {0.03333,0.03333,0.06369,0.03333,0.03333 }



输入文件:


Input file:

DATA_MODE,1A
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
24,0.06369
24,0.06369
24,0.06369
24,0.06369
24,0.06369
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333
5,0.03333




Co取自OP评论:



Code taken from OP comment:

// Read Rdm1.csv

float ReadFile(float Elastmod[], int NumLines,float Elastmod3[])
{
    ifstream myReadFile;
    char inputfile[_MAX_FNAME ],
    Extension[ _MAX_EXT ] = ".CSV";

    _makepath(Infilename, "C:", Dir, inputfile, Extension);

    myReadFile.open("Rdm1.csv");

    char* pch; //DYNAMICALLY ASSIGN ARRAY
    //pch= new char[NumLines];

    char* pch1; //DYNAMICALLY ASSIGN ARRAY
    pch1= new char[NumLines];

    char* Token; //DYNAMICALLY ASSIGN ARRAY
    Token = new char[NumLines];

    int i;
    if (myReadFile.is_open()) 
    {
        while (!myReadFile.eof()) 
        {
            myReadFile.ignore(256,'\n');  // to ignore first Line
	
            myReadFile.getline(Token,256,',');

            for(int i=0;i<(NumLines-1);i++)
            {
                pch = strtok (Token,",");

                myReadFile.getline(Token,256,',');

                float test = 0.04166;

                Elastmod[i]= atof(pch) ;

                if (  Elastmod[i] > test)
                    Elastmod3[i]= 15.7;
                else if ( Elastmod[i] == test)
                    Elastmod3[i]= 24;
                else
                    Elastmod3[i]= 30;


           }

        }
    }
    else
    {
       //cout << "Error opening file";
    }

    myReadFile.close();

    return Elastmod3[NumLines]  ;
}

推荐答案

读取每行文本,并使用其中一个拆分或标记函数来分隔字段。然后,您可以保存您感兴趣的项目并丢弃任何其他项目。
Read each line of text, and use one of the splitting or tokenise functions to separate the fields. You can then save the items you are interested in and discard any others.


这篇关于使用getline读取文件时跳过行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆