473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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,ab*@yahoo.com,2552 423";
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();
}
}

Mar 25 '07 #1
13 2148
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.
Mar 25 '07 #2
On 25 Mar, 23:05, red floyd <no.s...@here.dudewrote:
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.
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

Mar 25 '07 #3

"anant" <an**********@googlemail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
On 25 Mar, 23:05, red floyd <no.s...@here.dudewrote:
>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>

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
Mar 25 '07 #4

"Mike Wahler" <mk******@mkwahler.netwrote in message
news:f9*********************@newsread2.news.pas.ea rthlink.net...
>
"anant" <an**********@googlemail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
>On 25 Mar, 23:05, red floyd <no.s...@here.dudewrote:
>>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
Mar 25 '07 #5
"anant" <an**********@googlemail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
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,ab*@yahoo.com,2552 423";
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
Mar 26 '07 #6
On 26 Mar, 01:00, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"Mike Wahler" <mkwah...@mkwahler.netwrote in message

news:f9*********************@newsread2.news.pas.ea rthlink.net...


"anant" <anantniga...@googlemail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
On 25 Mar, 23:05, red floyd <no.s...@here.dudewrote:
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..

Mar 26 '07 #7

"anant" <an**********@googlemail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
On 26 Mar, 01:00, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"Mike Wahler" <mkwah...@mkwahler.netwrote in message

news:f9*********************@newsread2.news.pas.e arthlink.net...


"anant" <anantniga...@googlemail.comwrote in message
news:11**********************@e65g2000hsc.googleg roups.com...
On 25 Mar, 23:05, red floyd <no.s...@here.dudewrote:
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
>

Mar 26 '07 #8
On 27 Mar, 01:35, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"anant" <anantniga...@googlemail.comwrote in message

news:11**********************@b75g2000hsg.googlegr oups.com...
On 26 Mar, 01:00, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"Mike Wahler" <mkwah...@mkwahler.netwrote in message
>news:f9*********************@newsread2.news.pas.e arthlink.net...
"anant" <anantniga...@googlemail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
On 25 Mar, 23:05, red floyd <no.s...@here.dudewrote:
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

Mar 27 '07 #9

"anant" <an**********@googlemail.comwrote in message
news:11*********************@e65g2000hsc.googlegro ups.com...
>
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
Mar 27 '07 #10
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 27 '07 #11
On 27 Mar, 07:11, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"anant" <anantniga...@googlemail.comwrote in message

news:11*********************@e65g2000hsc.googlegro ups.com...


I have simply copied your given program to test it and created a same
name i.e "data.txt" namefilewithin 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 << "Errorreadinginput\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 thefileat right place, i think i am but dont knw...
waiting to sort this problem.....
Thanks a lot...

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

Mar 27 '07 #12
On 26 Mar, 02:00, "Jim Langston" <tazmas...@rocketmail.comwrote:
"anant" <anantniga...@googlemail.comwrote in message

news:11*********************@y80g2000hsf.googlegro ups.com...


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,a...@yahoo.com,255 2423";
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..

Mar 27 '07 #13

"anant" <an**********@googlemail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
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

Mar 27 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: Jean-Marie Vaneskahian | last post by:
Reading - Parsing Records From An LDAP LDIF File In .Net? I am in need of a .Net class that will allow for the parsing of a LDAP LDIF file. An LDIF file is the standard format for representing...
1
by: syhzaidi | last post by:
How can we do Parsing of Hexdecimel in C# reading string from stream file for eg.. i have a file like.......... 0f 2f 12 2d 3a.......in hexa decimal save in a file.txt and i m reading it from...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.