I've values in a csv file and I'd like to load these values into an array in C++ to perform some processes on them.
example of csv file:
10
2
1
3
4
I want to assign x[1]=10 , x[2]=2 ... etc
any help would be appreciated.
Thanks
Actually it's two columns but I don't want to consider the first one
Example:
April,10
May,2
Etc...
I want to load the second column to the array, not as a string since I'll need to perform some analysis on the numbers
If you can "carry on analysis", then you know C++ somewhat. How can it be that you are not familiar with basic IO? It tends to be first among things that one learns.
Write what you can and show it to us. Then we can proceed.
Great, Thanks you guys. It really helped me a lot.
I have a question please.
The file I got is like this:
1 2 3
1,2
3,4
5,6
I wanna ignore the first column and just extract the second, could anyone show me how to mess with the getline code to do that?
The following code reads the first column great, but I need to ignore the first column and read only the second one.
Extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character).
You write up to 256 characters to an array that has space for only 20 characters. You are lucky, because the lines in your input are shorter than 20 characters. If they were not, you would have undefined behaviour.
You don't read first column only. You read whole lines.
1 2 3 4 5 6
long value;
while ( i < 100 && infile.getline(256, ',') && infile >> value )
{
array[i] = value;
++i;
}