reading csv file.....help me pls...........

A

anant

Hi all
The below code is reading string and then tokenizin it and reading all
the info. But i want to call a csv file and it should then read a
string from dt file. So what midification should i do in the below
code. Its main code, all the functions are defined well. So just wanna
know how to include some extra code to open the file and read each
line.
Thanks a lot
typedef struct
{
string firstName;
string surname;
unsigned int HouseNumber;
string StreetName;
string Town;
unsigned int PostCode;
string email;
unsigned int Phone;
}Person;

int main(int argc, char* argv[])
{


string data = "James,Bond,
33,AlberStreet,Manchester,HG231,[email protected],2552423";
string delimiter = ",";
StringTokenizer strtok(data,delimiter);
StringTokenizer strtok(data,delimiter);

if(strtok.countTokens() != 8)
{
cout << "!-Error-! Not enough tokens!" << std::endl;
}
else
{
Person person;
person.firstName = strtok.nextToken();
person.surname = strtok.nextToken();
person.HouseNumber = strtok.nextIntToken();
person.StreetName = strtok.nextToken();
person.Town = strtok.nextToken();
person.PostCode = strtok.nextIntToken();
person.email = strtok.nextToken();
person.Phone = strtok.nextIntToken();
}


}
 
R

red floyd

anant said:
[redacted]

That's not working code. What is StringTokenizer? Please post a
minimal, compilable example along with the output you are getting, and
the expected output.
 
A

anant

anant said:
[redacted]

That's not working code. What is StringTokenizer? Please post a
minimal, compilable example along with the output you are getting, and
the expected output.

oh its just to show wt i hv done so far...well i have defined all d
functions and its not possible to post dt here. So this pice of code
is workin and it reads firstname, lastname, house no and so on of the
comma seperated string.
But this is somethin that doesnt ful fill my requirement as i want it
to read a line from csv file and then within that line read each
character seperated by comma.
or if you have a good solution to it then lemme know. Or if you could
modify this prog...you can assume the functions already defined. Well
if you say i can post it all here. But it gonna be long code then .
Thanks for replying
 
M

Mike Wahler

anant said:
anant said:
[redacted]

That's not working code. What is StringTokenizer? Please post a
minimal, compilable example along with the output you are getting, and
the expected output.

oh its just to show wt i hv done so far...well i have defined all d
functions and its not possible to post dt here. So this pice of code
is workin and it reads firstname, lastname, house no and so on of the
comma seperated string.
But this is somethin that doesnt ful fill my requirement as i want it
to read a line from csv file and then within that line read each
character seperated by comma.

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
int ret(EXIT_SUCCESS);

std::ifstream in("data.txt");

if(in)
{
std::streamsize ln(0);
std::string line;

while(std::getline(in, line))
{
std::cout << "Line " << ++ln << ": \n";
std::istringstream iss(line);
std::streamsize t(0);
std::string token;
const char delim(',');

while(std::getline(iss, token, delim))
{
std::cout << "\ttoken " << ++t << ": " << token << "\n";
}

if(!iss.eof())
{
std::cerr << "Error parsing tokens\n";
ret = EXIT_FAILURE;
}
}

if(!in.eof())
{
std::cerr << "Error reading input\n";
ret = EXIT_FAILURE;
}
}
else
{
std::cerr << "Cannot open input\n";
ret = EXIT_FAILURE;
}

return ret;
}


** Input:

abc,def,ghi
jkl,mno
pqr
stu,vw,xyz


** Output:

Line 1:
token 1: abc
token 2: def
token 3: ghi
Line 2:
token 1: jkl
token 2: mno
Line 3:
token 1: pqr
Line 4:
token 1: stu
token 2: vw
token 3: xyz



-Mike
 
M

Mike Wahler

Mike Wahler said:
anant said:
anant wrote:
[redacted]

That's not working code. What is StringTokenizer? Please post a
minimal, compilable example along with the output you are getting, and
the expected output.

oh its just to show wt i hv done so far...well i have defined all d
functions and its not possible to post dt here. So this pice of code
is workin and it reads firstname, lastname, house no and so on of the
comma seperated string.
But this is somethin that doesnt ful fill my requirement as i want it
to read a line from csv file and then within that line read each
character seperated by comma.

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

Also add:

#include <ios> // declares std::streamsize

-Mike
 
J

Jim Langston

anant said:
Hi all
The below code is reading string and then tokenizin it and reading all
the info. But i want to call a csv file and it should then read a
string from dt file. So what midification should i do in the below
code. Its main code, all the functions are defined well. So just wanna
know how to include some extra code to open the file and read each
line.
Thanks a lot
typedef struct
{
string firstName;
string surname;
unsigned int HouseNumber;
string StreetName;
string Town;
unsigned int PostCode;
string email;
unsigned int Phone;
}Person;

int main(int argc, char* argv[])
{


string data = "James,Bond,
33,AlberStreet,Manchester,HG231,[email protected],2552423";
string delimiter = ",";
StringTokenizer strtok(data,delimiter);
StringTokenizer strtok(data,delimiter);

if(strtok.countTokens() != 8)
{
cout << "!-Error-! Not enough tokens!" << std::endl;
}
else
{
Person person;
person.firstName = strtok.nextToken();
person.surname = strtok.nextToken();
person.HouseNumber = strtok.nextIntToken();
person.StreetName = strtok.nextToken();
person.Town = strtok.nextToken();
person.PostCode = strtok.nextIntToken();
person.email = strtok.nextToken();
person.Phone = strtok.nextIntToken();
}


}

Personally, I use a class I downloaded called CSVParser written by Mayukh
Bose which comes in source form. I've modified it a little which is easy to
do to add some types it didnt' have (boolean, etc..).
http://www.mayukhbose.com/freebies/c-code.php
 
A

anant

anant said:
anant wrote:
[redacted]
That's not working code. What is StringTokenizer? Please post a
minimal, compilable example along with the output you are getting, and
the expected output.
oh its just to show wt i hv done so far...well i have defined all d
functions and its not possible to post dt here. So this pice of code
is workin and it reads firstname, lastname, house no and so on of the
comma seperated string.
But this is somethin that doesnt ful fill my requirement as i want it
to read a line fromcsvfileand then within that line read each
character seperated by comma.
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

Also add:

#include <ios> // declares std::streamsize

-Mike

Hi Mike,
The program you mentioned is not working. I have given the file name
and still it directly jumps to cannot open input.
What could be wrong. ..i tried giving full path aswell but its nt
working...any ideas..
 
M

Mike Wahler

anant said:
anant wrote:
[redacted]
That's not working code. What is StringTokenizer? Please post a
minimal, compilable example along with the output you are getting,
and
the expected output.
oh its just to show wt i hv done so far...well i have defined all d
functions and its not possible to post dt here. So this pice of code
is workin and it reads firstname, lastname, house no and so on of the
comma seperated string.
But this is somethin that doesnt ful fill my requirement as i want it
to read a line fromcsvfileand then within that line read each
character seperated by comma.
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

Also add:

#include <ios> // declares std::streamsize

-Mike

Hi Mike,
The program you mentioned is not working. I have given the file name
and still it directly jumps to cannot open input.

You need to show exactly what you tried. THe code I posted has
a file name hard coded, so you could not have 'given the file name'.
Did you change the code to some other hard-coded name? Did you change
the code to acquire the file name from an external source (e.g. with
'cin' or from the command line)? In either case, have you made sure
the file actually exists and is accessible (e.g. not locked by another
process, etc.)?
What could be wrong. ..i tried giving full path aswell but its nt
working...any ideas..

Show *exactly* what you've done.

-Mike
 
A

anant

anant wrote:
[redacted]
That's not working code. What is StringTokenizer? Please post a
minimal, compilable example along with the output you are getting,
and
the expected output.
oh its just to show wt i hv done so far...well i have defined all d
functions and its not possible to post dt here. So this pice of code
is workin and it reads firstname, lastname, house no and so on of the
comma seperated string.
But this is somethin that doesnt ful fill my requirement as i want it
to read a line fromcsvfileand then within that line read each
character seperated by comma.
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
Also add:
#include <ios> // declares std::streamsize
-Mike
Hi Mike,
The program you mentioned is not working. I have given thefilename
and still it directly jumps to cannot open input.

You need to show exactly what you tried. THe code I posted has
afilename hard coded, so you could not have 'given thefilename'.
Did you change the code to some other hard-coded name? Did you change
the code to acquire thefilename from an external source (e.g. with
'cin' or from the command line)? In either case, have you made sure
thefileactually exists and is accessible (e.g. not locked by another
process, etc.)?
What could be wrong. ..i tried giving full path aswell but its nt
working...any ideas..

Show *exactly* what you've done.

-Mike

I have simply copied your given program to test it and created a same
name i.e "data.txt" name file within project folder.
with a data exactly as you mentioned i.e.
abc,def,ghi
jkl,mno
pqr
stu,vw,xyz

And the code i copied is

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <ios> // declares std::streamsize

int main()
{
int ret(EXIT_SUCCESS);


std::ifstream in("data.txt");


if(in)
{
std::streamsize ln(0);
std::string line;


while(std::getline(in, line))
{
std::cout << "Line " << ++ln << ": \n";
std::istringstream iss(line);
std::streamsize t(0);
std::string token;
const char delim(',');


while(std::getline(iss, token, delim))
{
std::cout << "\ttoken " << ++t << ": " << token <<
"\n";
}


if(!iss.eof())
{
std::cerr << "Error parsing tokens\n";
ret = EXIT_FAILURE;
}
}


if(!in.eof())
{
std::cerr << "Error reading input\n";
ret = EXIT_FAILURE;
}
}
else
{
std::cerr << "Cannot open input\n";
ret = EXIT_FAILURE;
}


return ret;

}

After readinf if(in) statement it directly jumps to cannot open
input...
I have tried this out in both Borland 6.0 and MSVstudio2005 (win32
console)
I dont know whats the problem. I have tried various different ways
aswell but none of them are working.
Am i placing the file at right place, i think i am but dont knw...
waiting to sort this problem.....
Thanks a lot...
anant
 
M

Mike Wahler

anant said:
I have simply copied your given program to test it and created a same
name i.e "data.txt" name file within project folder.
with a data exactly as you mentioned i.e.
abc,def,ghi
jkl,mno
pqr
stu,vw,xyz

And the code i copied is

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <ios> // declares std::streamsize

int main()
{
int ret(EXIT_SUCCESS);


std::ifstream in("data.txt");


if(in)
{
std::streamsize ln(0);
std::string line;


while(std::getline(in, line))
{
std::cout << "Line " << ++ln << ": \n";
std::istringstream iss(line);
std::streamsize t(0);
std::string token;
const char delim(',');


while(std::getline(iss, token, delim))
{
std::cout << "\ttoken " << ++t << ": " << token <<
"\n";
}


if(!iss.eof())
{
std::cerr << "Error parsing tokens\n";
ret = EXIT_FAILURE;
}
}


if(!in.eof())
{
std::cerr << "Error reading input\n";
ret = EXIT_FAILURE;
}
}
else
{
std::cerr << "Cannot open input\n";
ret = EXIT_FAILURE;
}


return ret;

}

After readinf if(in) statement it directly jumps to cannot open
input...
I have tried this out in both Borland 6.0 and MSVstudio2005 (win32
console)
I dont know whats the problem. I have tried various different ways
aswell but none of them are working.
Am i placing the file at right place, i think i am but dont knw...
waiting to sort this problem.....
Thanks a lot...

It sounds like either the data file isn't in the 'right place'
or your OS is for some reason disallowing access. Try putting
both the executable and the data file in the same 'path' ('directory'
or 'folder'), and running it from there.

-Mike
 
A

Alf P. Steinbach

* anant:
I have simply copied your given program to test it and created a same
name i.e "data.txt" name file within project folder.

Hello, "project", "folder", that should tell you something.

You're not working at the command line, are you?

You're working in some IDE, aren't you?

It probably places the executable in some subdirectory, or the "folder"
is something in the IDE, not directly representing a directory.

Get thee out of that IDE and down to the command line, promptly.
 
A

anant

It sounds like either the datafileisn't in the 'right place'
or your OS is for some reason disallowing access. Try putting
both the executable and the datafilein the same 'path' ('directory'
or 'folder'), and running it from there.

-Mike- Hide quoted text -

- Show quoted text -

YEH u r right i guess because now i have tested it over another system
and it worked fine.
I dont know why its not working on my system
Is there anything i could do to make it working.
And yeh pls also tell me the command to hold the screen. I have added
system("pause"); but thats not working with the program you mentioned.
I mean there is no error but its not holding the screen
Thanks a lot
 
A

anant

Hi all
The below code isreadingstring and then tokenizin it andreadingall
the info. But i want to call acsvfileand it should then read a
string from dtfile. So what midification should i do in the below
code. Its main code, all the functions are defined well. So just wanna
know how to include some extra code to open thefileand read each
line.
Thanks a lot
typedef struct
{
string firstName;
string surname;
unsigned int HouseNumber;
string StreetName;
string Town;
unsigned int PostCode;
string email;
unsigned int Phone;
}Person;
int main(int argc, char* argv[])
{
string data = "James,Bond,
33,AlberStreet,Manchester,HG231,[email protected],2552423";
string delimiter = ",";
StringTokenizer strtok(data,delimiter);
StringTokenizer strtok(data,delimiter);
if(strtok.countTokens() != 8)
{
cout << "!-Error-! Not enough tokens!" << std::endl;
}
else
{
Person person;
person.firstName = strtok.nextToken();
person.surname = strtok.nextToken();
person.HouseNumber = strtok.nextIntToken();
person.StreetName = strtok.nextToken();
person.Town = strtok.nextToken();
person.PostCode = strtok.nextIntToken();
person.email = strtok.nextToken();
person.Phone = strtok.nextIntToken();
}

Personally, I use a class I downloaded called CSVParser written by Mayukh
Bose which comes in source form. I've modified it a little which is easy to
do to add some types it didnt' have (boolean, etc..).http://www.mayukhbose.com/freebies/c-code.php- Hide quoted text -

- Show quoted text -

Hi
just now i tested it here at work and its working fine . I dont know
whats wrong in my pc. May be my OS is not allowing it to read the
file.
Can you think of any problem related to this.
Well i really need to sort this out at ma laptop. I wasted hell lotta
time over this and the problem was something else. huh..
And yeh cn you also mention the command to hold the screen for
output.
Well i am trying out with stystem("pause"); but it isnt working .
Is there any thing else for this.
Thanks a lot..
 
M

Mike Wahler

And yeh pls also tell me the command to hold the screen. I have added
system("pause"); but thats not working with the program you mentioned.
I mean there is no error but its not holding the screen

I don't know if your system has a 'pause' command, and if
it does, what it does. Two other things to try:

1. Run the program from the command line, so there's no IDE
'window' to close.

2. At the point you want the program to pause, write:

std::cin.get();

-Mike
 

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

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top