Click here to Skip to main content
15,946,158 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to skip lines while reading .csv file. using getline?

Right now it is storing all lines into array.(storing 2nd column into array)
C++
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  }

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

Required output:
C++
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


Code taken from OP comment:
C++
// 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]  ;
}
Posted
Updated 13-Jan-14 7:36am
v3
Comments
OriginalGriff 13-Jan-14 12:15pm    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Perhaps the fragment of code you are currently using to read the data would help us?
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 14-Jan-14 5:50am    
You are getting a token pointer from the dataline that you read in, and then immediately overwrite the data by calling getline a second time. Your for loop is repeating something 256 times for no logical reason.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900