字符串操作问题 [英] Problem with string manipulation

查看:81
本文介绍了字符串操作问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们


我正在尝试编写一个像p2p服务器一样的程序。当

程序启动时,它会读取一个文件,从那里读取广播IP

地址,端口和另一个端口号。现在我试着跳过

评论和空行,说明是否有换行符号或#符号在一行的第一个字符,然后跳过该行。但是问题是

程序无效。


我使用以下代码:


line = malloc (sizeof(char)* SIZE_OF_LINE);


while(fgets(line,SIZE_OF_LINE + 1,fp)!= NULL)

{

if(line [0]!=''#''|| line [0]!=''\ n'')

{

fprintf (stderr,我在这里);

fprintf(stderr,"%s",line);

strcpy(tmpEntry,strtok(line) ,":"));

strcpy(tmpValue,strtok(NULL," \ n"));

fprintf(stderr," \ t%s",tmpEntry);

fprintf(stderr," \t:%s \ n",tmpValue);

}

}


和程序正在阅读的文件是:


############# ####################### #########################################

# P2P配置文件

#请不要修改此文件,因为这是文件用于

#create所有必需的套接字

#Author :Ahetesham Qazi

####################################### ######################################


Brodcast_IP:192.168.1.255

Brodcast_Port:30001

Comm_Port:30101


程序应该跳过前七行但是问题它不是跳过b
$ b。当它试图标记第一行然后

打印它时,它给了我分段错误。


任何建议都会大大增加赞赏

谢谢

Ahetesham Qazi

Hi folks

I am trying to write a program which acts like a p2p server. When the
program starts it reads a file from whereit will read broadcast IP
address, port and another port number. Now I am trying skip the
comments and empty lines by saying if there is newline or # sign at the
first charecter of a line then to skip the line.But the problem is the
program is not working.

I am using the following code:

line = malloc(sizeof (char) *SIZE_OF_LINE);

while(fgets(line, SIZE_OF_LINE + 1, fp) != NULL)
{
i f(line[0] != ''#'' || line[0] != ''\n'')
{
fprintf(stderr,"i am here\n");
fprintf(stderr,"%s", line);
strcpy(tmpEntry, strtok(line, ":"));
strcpy(tmpValue, strtok(NULL, "\n"));
fprintf(stderr,"\t%s", tmpEntry);
fprintf(stderr,"\t: %s\n", tmpValue);
}
}

and the file the program is reading is:

################################################## ###########################
# P2P Conf File
#Please Don''t modify this file as this is file is used to
#create all required sockets
# Author : Ahetesham Qazi
################################################## ###########################

Brodcast_IP:192.168.1.255
Brodcast_Port:30001
Comm_Port:30101

The program should skip the first seven lines but the problem it''s not
skipping. and when it''s trying to tokenize the first line and then
printing it it''s giving me segmentation fault.

Any suggetion would be greatly appreciated
Thanks
Ahetesham Qazi

推荐答案

aq *** @ inbox.com 说:

if(line [0]!= '''''|| line [0]!=''\ n'')
if(line[0] != ''#'' || line[0] != ''\n'')



你能想到多少个字符会导致这种情况失败?


大声读出来。如果这个字符不是哈希值,或者它不是换行符...


所以哈希通过测试,因为它不是换行符,换行符PASSES

测试因为它不是哈希值。


你打算使用&&而不是||


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

How many characters can you think of that will fail this condition?

Read it out loud. If this character is not a hash, OR it is not a newline...

So a hash PASSES the test because it is not a newline, and a newline PASSES
the test because it is not a hash.

You meant to use && rather than ||

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


>我正在尝试编写一个程序其作用类似于p2p服务器。当
>I am trying to write a program which acts like a p2p server. When the

>程序启动时,它会读取一个文件,从那里读取广播IP地址,端口和另一个端口号。现在我尝试跳过
注释和空行,说明在第一行的第一个字符中是否有换行符或#符号然后跳过该行。
>program starts it reads a file from whereit will read broadcast IP
address, port and another port number. Now I am trying skip the
comments and empty lines by saying if there is newline or # sign at the
first charecter of a line then to skip the line.



....

....


>我使用以下代码:

line = malloc(sizeof(char)* SIZE_OF_LINE);

while(fgets(line,SIZE_OF_LINE + 1,fp)!= NULL)
{

if(line [0]!=''#''|| line [0]!=''\ n'')
>I am using the following code:

line = malloc(sizeof (char) *SIZE_OF_LINE);

while(fgets(line, SIZE_OF_LINE + 1, fp) != NULL)
{
i f(line[0] != ''#'' || line[0] != ''\n'')



换行不是等于''#''。

A#不等于''\ n''。

另一个角色不等于。

换句话说,条件总是正确的。


你真的想在这里使用OR吗?

A newline is not equal to ''#''.
A # is not equal to ''\n''.
Another character is not equal to either.
In other words, the condition is always true.

Do you really want to use OR here?




Richard Heathfield写道:

Richard Heathfield wrote:
aq *** @ inbox.com 说:

if(line [0]!=''#''|| line [0]!=''\ n'')
if(line[0] != ''#'' || line[0] != ''\n'')



你能想到多少个字符会导致这种情况失败?

大声读出来。如果这个字符不是哈希值,或者它不是换行符...


所以哈希通过测试,因为它不是换行符,换行符PASSES

测试因为它不是哈希值。


你打算使用&&而不是||


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


How many characters can you think of that will fail this condition?

Read it out loud. If this character is not a hash, OR it is not a newline...

So a hash PASSES the test because it is not a newline, and a newline PASSES
the test because it is not a hash.

You meant to use && rather than ||

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)



感谢您的解决方案。实际上这是一个严格预定义的文件。

所以没有实际数据

唯一的字符可以在文件的开头就是#。或\ n

Ahetesham Qazi

Thanks for the solution. Actually this is a strictly predefined file.
So whithout the actual data
the only charecter can be at the start of the file is "#" or "\n"
Ahetesham Qazi


这篇关于字符串操作问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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