CSV file conversion...

A

Adam Balgach

Greetings all, i am trying to parse up a CSV Comma delmited file from
microsoft (gasp) excel. And then use that data to convert it to
another file format (.arff). in any event, i am having a bunch of
trouble trying to get the individual lines. If i open the .csv in
emacs then it appears that there are eoln characters (/n) however,
when i attempt to use iostream::getline(char*, int, char) it seems to
be failing on finding the end of line. im using the code:

ifstream input;
input.open("test_data.csv");
if (!input.good()) {
cerr << "Error opening\n";
exit(0);
}
char line[1024];
input.getline(line,1024,'\t'); //gets the first line in the file
int pos=0;
cout << "First Line: "<<line;


what its basically doing is reading past the first end of line and
reading exactly 1024 characters. Has anyone worked with csv files
before? Please let me know. Thanks!

Cheers,
Adam.
 
J

Jack Klein

Greetings all, i am trying to parse up a CSV Comma delmited file from
microsoft (gasp) excel. And then use that data to convert it to
another file format (.arff). in any event, i am having a bunch of
trouble trying to get the individual lines. If i open the .csv in
emacs then it appears that there are eoln characters (/n) however,
when i attempt to use iostream::getline(char*, int, char) it seems to
be failing on finding the end of line. im using the code:

ifstream input;
input.open("test_data.csv");
if (!input.good()) {
cerr << "Error opening\n";
exit(0);
}
char line[1024];
input.getline(line,1024,'\t'); //gets the first line in the file
int pos=0;
cout << "First Line: "<<line;


what its basically doing is reading past the first end of line and
reading exactly 1024 characters. Has anyone worked with csv files
before? Please let me know. Thanks!

Cheers,
Adam.

I think your problem is a file format one, not a C++ one. There are
no tab characters ('\t') in a CSV (Comma Separated) file, although you
can have Excel export tab delimited files as well.

You need to look for ',', not for '\t'.
 
R

Real Name

Hi,

[snip]
emacs then it appears that there are eoln characters (/n) however,
when i attempt to use iostream::getline(char*, int, char) it seems to
be failing on finding the end of line. im using the code:
[/snip]

DOS/Windows uses the old TTY-style for EOL: 0x0d0x0a (or vv); *ix uses '\n'.
So what you expect as EOL is not EOL.
 
P

Phil Staite

You're getting input up to 1024 characters or until a tab character?
That's not necessarily the first "line" of the file. Also, what if a
line > 1024 chars?

How about using std::string? Something like:


std::string line;
std::getline( input, line, '\n' );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top