Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I read one csv file in which it contains different types of data like string, integer etc.I want to fetch only integer data from that file and store in vector.Below i copy some code that read line from file,store in vector.I tried to convert string to integer or double and do some operations on it as i know that string values,but how to identify only integer values and store that in vector?

C++
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<sstream>
#include<iterator>
#include<algorithm>
using namespace std;

int main() 
{
	string line;
	int cnt=0,val1,val2;
	
	vector<string> vf;
	
	ifstream in("TestValues.csv");
	ofstream out("result.csv");

	while(getline(in,line,','))
	{
		vf.push_back(line);
	}	
	
	for(int i=0;i<vf.size();i++)
	{	
		val1=atoi(vf[i].c_str());
		out<<val1;
	}	

	in.close();
	out.close();
	system("pause");

	return 0;
}

i/p file data like
CSS
Employee Code:      121A    Employee Name :   David
    Date      InTime  OutTime Shift  Total Duration    Status
    01-Apr-02  9:59    19:53   FS        9:53         Present


I want to deal with only Employee code to sort record of Intime,OutTime and Total Duration of working of employee in that month.How to do that?
Posted
Comments
Rage 14-Mar-14 6:36am    
How does the input looks like in the file (please leave the carraige returns and comas) ?

Venkat

When you access the csv file your first value is Employee Code, thus we can assume a relationship as
First Value -- Employee Code
2 Value -- Employee Name
3 Value -- Date

and so on to

8 Value -- Status

So every 9th position after the first is your Employee Code. Similarly you can devise that every 9 the position after 4th is your InTime.

This way you can access each value and maintain vectors and do what ever you wish with them.

Hope this helps
 
Share this answer
 
Comments
Rage 14-Mar-14 6:35am    
It is probably a tad more complicated, since he is reading in line by line, not value by value.
vikram_bullet 19-Mar-14 3:25am    
I guess whenever we read from a csv file, the values are either ; seperated or tab seperated. And in such a case we can easily loop through all the values.
My first question is how the data file continues. Does it continue with another three-line entry for another employee, or does it continue with further dates for the same employee (i.e. there is only one employee per file)?

Either way, you'll have to parse the string you read in. I wrote a couple of utility classes for splitting strings that you might find useful:
Splitting strings
Splitting strings again – strtok redeemed

Assuming that each file only has one empolyee, many dates, and that the format is consistent, I'd do something like this:

// Open file
...

// Read employee line
string line;
string empCode;
if (!getline(in,line))
{
    // Error
    return -1;
}
else
{
    // Get employee code using my string tokeniser
    // from "Splitting strings again – strtok redeemed"
    string_tokeniser tokeniser(line);
    // First token "Employee Code:" ends with colon
    tokeniser.next(':');
    // Employee code ends here
    tokeniser.next("Employee Name :");
    tokeniser.get_token(empCode);
    // empCode now contains "      121A    "
    // trim spaces to taste
}
// Next line is header, so read and throw away
if (!getline(in,line))
{
    // Error
    return -1;
}
// Now we come to the meat.
while (getline(in,line))
{
    // Split the line up into substrings
    vector<string> lineElements;
    // Using the string splitter from "Splitting strings"
    // to split on space and ignore empty substrings
    tokenise_string(line, ' ', lineElements);
    // Should have six fields
    // (date, in, out, shift, total, status)
    if (lineElements.size() < 6)
    {
        return -1;
    }
    // Write to output file
    out << empCode << ',' << lineElements[1] << ',' <<
         lineElements[2] << ',' <<  lineElements[4] << endl;
}
 
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