阅读csv文件.....请帮帮我........... [英] reading csv file.....help me pls...........

查看:68
本文介绍了阅读csv文件.....请帮帮我...........的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

下面的代码是读取字符串然后标记它并阅读所有

的信息。但我想调用一个csv文件,然后它应该从dt文件中读取一个

字符串。那么我应该在下面的

代码中做什么调整。它的主要代码,所有功能都定义得很好。所以只想要b / b $ b知道如何包含一些额外的代码来打开文件并阅读每一行

行。

非常感谢

typedef struct

{

string firstName;

string surname;

unsigned int HouseNumber; < br $>
string StreetName;

string Town;

unsigned int PostCode;

string email;

unsigned int电话;

}人;


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<< "! - 错误 - !没有足够的令牌! << std :: endl;

}

else

{

人员;

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();

}

}

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();
}
}

推荐答案

anant写道:
anant wrote:


[redacted]



这不是正常工作的代码。什么是StringTokenizer?请发布一个

最小,可编辑的示例以及您获得的输出,并且

预期输出。

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.


3月25日23:05,red floyd< no.s ... @ here.dudewrote:
On 25 Mar, 23:05, red floyd <no.s...@here.dudewrote:

anant写道:
anant wrote:


[redacted]



这不是正常工作的代码。什么是StringTokenizer?请发布一个

最小,可编辑的示例以及您获得的输出,并且

预期输出。


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.



哦它只是为了显示我迄今为止完成的wt ...我已经定义了所有d

函数并且它不可能发布dt在这里。所以这段代码

是workin,它读取了

逗号分隔字符串的名字,姓氏,房子号等。

但是这是不能满足我的要求,因为我想要它

从csv文件中读取一行,然后在该行内读取每个由逗号分隔的
字符。 />
或者如果你有一个很好的解决方案,那么lemme就知道了。或者,如果你可以修改这个编程...你可以假定已定义的函数。那么

如果你说我可以在这里发布。但是它会有很长的代码。

感谢您的回复

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




" anant" < a ********** @ googlemail.comwrote in message

news:11 ******************** **@e65g2000hsc.googlegr oups.com ...

"anant" <an**********@googlemail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...

3月25日23:05,red floyd< no.s ... @ here.dudewrote :
On 25 Mar, 23:05, red floyd <no.s...@here.dudewrote:

> anant写道:
>anant wrote:


[redacted]

<这不是有效的代码。什么是StringTokenizer?请发布一个最小的,可编辑的示例以及您获得的输出,以及
预期输出。


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.



哦它只是为了显示我迄今为止完成的wt ...我已经定义了所有d

函数并且它不可能发布dt在这里。所以这段代码

是workin,它读取了

逗号分隔字符串的名字,姓氏,房子号等。

但是这是不能满足我的要求,因为我想要它

从csv文件读取一行然后在该行内读取每个由逗号分隔的
字符。


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<< 线 << ++ 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<< : <<令牌<< " \ n";

}


if(!iss.eof())

{

std :: cerr<< 解析令牌时出现错误\ n;;

ret = EXIT_FAILURE;

}

}


if(!in.eof())

{

std :: cerr<< 读取输入错误\ n;

ret = EXIT_FAILURE;

}

}

else

{

std :: cerr<< 无法打开input \ n;

ret = EXIT_FAILURE;

}


返回ret;

}

**输入:


abc,def,ghi

jkl,mno

pqr

stu,vw,xyz

**输出:


第1行:

代币1:abc

代币2:def

代币3:ghi

第2行:

代币1:jkl

代币2:mno

第3行:

代币1:pqr

第4行:

代币1:stu

代币2:vw

代币3:xyz


-Mike

#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


这篇关于阅读csv文件.....请帮帮我...........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆