Input from csv file to C++

Pages: 12
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
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.
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
Yes, but if you're asking us how to parse the file, you have to show use the format.
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?
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
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.
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
http://www.cplusplus.com/doc/tutorial/files/

How do you know that the input table has exactly 50000 rows?
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
Thanks a lot loonielou.

But I have a problem including cstdlib, it says "Unable to open include file CSTDLIB.h"
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
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?
Use the gnu compiler instead.
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/
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
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.
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
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;
}
But I wanna ignore anything beyond the first comma!
Last edited on
Pages: 12