Working on a CSV file

hi everybody,

I'm new to C++ programming language, and recently I've been asked to work big tables of data that is registed in text files that are under .csv format,,,,

My problem is HOW CAN I USE THE DATA THAT IS OF FLOAT TYPE from a csv file ??

I've got an idea but i couldn't realise it, i thought of reading the file line bye line, but I don't know how to transform the string of characters into floating point numbers and also how to get rid of the commas that separate the numbers,,,,

I'll be gratful for any help, and thanks in advance
Why not use atof? Get the input at a string and then do

float x=atof(Stringname, 10);

It'll make a float out of it.


To get the comma out (you should do this first) use strlen

Stringname[strlen(Stringname)]=" "; //voids the last character javascript:editbox1.editSend()of input

http://www.cplusplus.com/reference/clibrary/cstring/strlen/

http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
@NerdTastic:
What's wrong with C++ strings? The functions you're using are for C strings...

@nadir CoCo:
If your commas are also in the lines as opposed to at the end of them, then I'd suggest using find_first_of and substr.

http://cplusplus.com/reference/string/string/find_first_of/
http://cplusplus.com/reference/string/string/substr/

Good luck!
-Albatross

C strings are fine.

You could also use spanexcluding to get rid of commas within lines but i haven't used that in ages and don't remember syntax at all haha. It's on here somewhere
Last edited on
Yes, but C++ strings:
*Take care of all their memory management themselves.
*Have more intuitively-defined operators.
*Support a greater range of operations on them.
*Are more in accordance with common C++ methodologies.

Food for thought.

-Albatross
Last edited on
There are much better ways to tokenize strings. For example, getline takes a third parameter (delimiter) and works well with stringstreams:
http://cplusplus.com/reference/string/getline/
http://www.cplusplus.com/reference/iostream/stringstream/

Here's a cool thread on CSVs:
http://www.cplusplus.com/forum/general/7385/

If you want to use the csv_istream_iterator approach, I can help you out. I wrote it.

Are the fields just numbers or are there strings, quoted strings, etc.?
Last edited on
Thanks for all of you for your sugestions,,,,,

i'll try them out and if i still have problem I'll reply later

@moorecm (1657): The first line of the file is a discription of each of the colums made of letters and numbers,,,,and all the rest is just numbers of type double,,,,and everything is separated by commas,,,,,also, there is nothig at the end of the line !

Untested suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <sstream>
using namespace std;
//...
fstream fin( "data.csv" );
string descriptions;
if( getline( fin, descriptions ) ) {
    string line;
    while( getline( fin, token, ',' ) ) {
        stringstream sin( line );
        double d;
        while( sin >> d ) {
            // process d (this example prints the doubles separated by spaces)
            cout << d << " ";
        }
        cout << endl;
    }
}
Thank you budies, I have succeeded to convert some sample file from CSV to TXT, you've been really very helpful :)
Topic archived. No new replies allowed.