Input from csv file to C++

Pages: 12
Aug 19, 2014 at 9:43pm
Hello guys.

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
Aug 20, 2014 at 8:19am
That doesn't look like a CSV file (although technically it is). Are you sure it only has 1 field per line? You have to be clear about these things.
Aug 20, 2014 at 9:28am
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

Thanks a lot
Last edited on Aug 20, 2014 at 9:29am
Aug 20, 2014 at 10:33am
Yes, but if you're asking us how to parse the file, you have to show use the format.
Aug 20, 2014 at 10:52am
Ignore up to comma, then read the integer. Rinse and repeat.

Note, indexing starts from 0, not 1 like you used in example. Furthermore, how large should your "array" be?
Aug 20, 2014 at 1:22pm
I just need help to write c++ code to load that column into an array of size 50000, and I'll carry on analyzing these numbers.
Thanks
Aug 20, 2014 at 1:38pm
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.
Aug 20, 2014 at 11:16pm
I do know C++ somewhat, but this one is new to me.

Here's the code so far:


ifstream non("non.csv" , ios::in);

int arr[50000];

for (int j = 0; j < 50000; j++) {

/* Some code here to cin the values into this array */ = arr[j];


}

non.close();
Last edited on Aug 20, 2014 at 11:21pm
Aug 21, 2014 at 6:26am
http://www.cplusplus.com/doc/tutorial/files/

How do you know that the input table has exactly 50000 rows?
Aug 21, 2014 at 7:43am
1
2
3
4
5
6
7
8
9
10
string str;

for (int j = 0; j < 50000; ++j) {

getline(non, str);

arr[j] = atoi(str.substr(str.find(',')).c_str());

}

This might be a bit buggy but you get the idea.. Wouldn't hurt with some error handling as well. Proper includes needed. <string> <cstdlib>
Last edited on Aug 21, 2014 at 7:46am
Aug 26, 2014 at 1:58pm
Thanks a lot loonielou.

But I have a problem including cstdlib, it says "Unable to open include file CSTDLIB.h"
Aug 26, 2014 at 2:10pm
in C++ it should be
 
#include <cstdlib> 


If you were using C rather than C++, it would be:
 
#include <stdlib.h> 


Also, to use C++ std::string, then #include <string> .
(<string.h> is for C-style null-terminated character strings).
Last edited on Aug 26, 2014 at 2:12pm
Aug 26, 2014 at 2:17pm
Yes I'm using Borland C++ and still won't work!

I looked into INCLUDE folder and didn't find the header file "cstdlib".

Do you think that I can I find it somewhere?
Aug 26, 2014 at 2:21pm
Use the gnu compiler instead.
Aug 26, 2014 at 2:27pm
Yes, the Borland compilers are very out of date, don't use them unless you absolutely have to (some college courses are still based around them).

Try code::blocks which will enable you to use a modern compiler
http://www.codeblocks.org/
Aug 30, 2014 at 3:02pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

int main()

{

ifstream infile;
long array[100];
int i=0;
char cNum[20] ;
                infile.open ("BE.txt", ifstream::in);

                if (infile.is_open())
                {
                        while (infile.good())
                        {
                                infile.getline(cNum, 256);  // I really don't know what 256 means!!
                                array[i]= atof(cNum) ;
                                i++ ;
                        }
                        infile.close();
                }
                else
                {
                        cout << "Error opening file";
                }
for (i=0;i<100;i++)
{
  cout << array[i] << endl;
}
getche ();
}
Last edited on Aug 30, 2014 at 3:04pm
Aug 30, 2014 at 6:52pm
infile.getline(cNum, 256); // I really don't know what 256 means!!

See http://www.cplusplus.com/reference/istream/istream/getline/

When you read the description of streamsize and then look at the character array that you use, you should notice a problem.

Next you could look at http://www.cplusplus.com/reference/cstdlib/atof/
Does the return type of atof and the type of your "array" match?

What if the file has more than 100 lines? Earlier you spoke of 50000.
Aug 30, 2014 at 8:58pm
It actually worked on 50000 lines, but I made it 100 to speed run trials.

I understood streamsize, thanks a lot.


I looked into the links you gave me, I think it should be
double arr[100]

instead of long arr[100]

The code reads the first column just fine, but I don't understand how to ignore the first column and read the second one.

Thanks
Last edited on Aug 30, 2014 at 9:00pm
Aug 30, 2014 at 9:06pm
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;
}
Aug 30, 2014 at 9:12pm
But I wanna ignore anything beyond the first comma!
Last edited on Aug 30, 2014 at 9:13pm
Pages: 12