strcmp但是'\ n'作为终止者 [英] strcmp but with '\n' as the terrminator

查看:216
本文介绍了strcmp但是'\ n'作为终止者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在将一个文件读入一个char数组,我想查找一个字符串是否存在
给定行中的


我不能使用strcmp,因为该行以''\ n''结尾,而不是''\ 0''。是否有类似的功能可以做到这一点,或者我必须自己编写?

谢谢

Allan

Hi there,
I am reading a file into a char array, and I want to find if a string exists
in a given line.
I cant use strcmp since the line ends with ''\n'' and not ''\0''. Is there a
similar function that will do this, or will I have to write my own?
Thanks
Allan

推荐答案

Allan Bruce写道:
Allan Bruce wrote:
你好,
我正在将文件读入char数组,并且我想查找给定行中是否存在字符串

我不能使用strcmp,因为该行以''\ n''结尾,而不是''\ 0''。是否有类似的功能可以做到这一点,还是我必须自己编写?
Hi there,
I am reading a file into a char array, and I want to find if a string
exists in a given line.
I cant use strcmp since the line ends with ''\n'' and not ''\0''. Is there a
similar function that will do this, or will I have to write my own?




假设由于一些奇怪的原因你没有然后切换到我的

"弹性字符串"例程(滥用单词string,因为它们在非空终止数据上工作

),这是最简单的方法来做你想要的,如果

"串QUOT;是可写的,是找到\ n,将其更改为\0,执行strstr,

然后再将其更改回来。如果你这么做很多,那么你应该小心,因为它不是一个非常有效的解决方案;在这种情况下,你会想要写自己的,我想(除非有人有更好的主意)。


-

Richard Heathfield: bi****@eton.powernet.co.uk
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton



Assuming that for some strange reason you haven''t yet switched over to my
"stretchy string" routines (which abuse the word "string", since they work
on non-null-terminated data), the easiest way to do what you want, if the
"string" is writeable, is to find the \n, change it to \0, do the strstr,
and then change it back again. If you''re doing this a lot, though, you
should beware, as it''s not a very efficient solution; in which case, you''d
want to write your own, I guess (unless someone has a better idea).

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton




" Allan Bruce" <人***** @ TAKEAWAYf2s.com>在消息中写道

新闻:bf ********** @ news.freedom2surf.net ...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:bf**********@news.freedom2surf.net...
你好,
我是将文件读入char数组,我想查找给定行中是否存在字符串

我不能使用strcmp,因为该行以''\ n''结尾而不是'' \0'。是否有类似的功能可以做到这一点,还是我必须自己编写?
Hi there,
I am reading a file into a char array, and I want to find if a string exists in a given line.
I cant use strcmp since the line ends with ''\n'' and not ''\0''. Is there a
similar function that will do this, or will I have to write my own?




如果你在字符串中寻找一个子字符串你可以使用

strstr()


语法:


#include< string.h>

char * strstr(const char * s1,const char * s2);


描述:


扫描一个字符串一个给定子串的出现。


strstr扫描s1第一次出现的子串s2。


返回值


strstr返回指向s1中元素的指针,其中s2开始(指向s2中的s2

)。如果在s1中没有出现s2,则strstr返回null。


HTH

cw



If you are looking for a substring in a string you can use
strstr()

Syntax:

#include <string.h>
char *strstr(const char *s1, const char *s2);

Description:

Scans a string for the occurrence of a given substring.

strstr scans s1 for the first occurrence of the substring s2.

Return Value

strstr returns a pointer to the element in s1, where s2 begins (points to s2
in s1). If s2 does not occur in s1, strstr returns null.

HTH
cw


Richard Heathfield写道:
Richard Heathfield wrote:
Allan Bruce写道:

Allan Bruce wrote:

你好,
我正在将文件读入char数组,而我想要查找给定行中是否存在字符串

我不能使用strcmp,因为该行以''\ n''结尾,而不是''\ 0''。是否有类似的功能可以做到这一点,还是我必须自己编写?
Hi there,
I am reading a file into a char array, and I want to find if a string
exists in a given line.
I cant use strcmp since the line ends with ''\n'' and not ''\0''. Is there a
similar function that will do this, or will I have to write my own?



假设由于一些奇怪的原因你还没有切换到我的
弹力绳子例程(滥用单词string,因为它们对非空终止数据起作用),这是最简单的方法来做你想要的,如果
string是可写的,是找到\ n,将其更改为\0,执行strstr,
然后再将其更改回来。但是,如果你这么做很多,那么你应该小心,因为它不是一个非常有效的解决方案;在这种情况下,你想写自己的,我猜(除非有人有更好的主意)。


Assuming that for some strange reason you haven''t yet switched over to my
"stretchy string" routines (which abuse the word "string", since they work
on non-null-terminated data), the easiest way to do what you want, if the
"string" is writeable, is to find the \n, change it to \0, do the strstr,
and then change it back again. If you''re doing this a lot, though, you
should beware, as it''s not a very efficient solution; in which case, you''d
want to write your own, I guess (unless someone has a better idea).




也许他可以忽略\ n而只是使用strstr()而不是?他不会得到整条线的完全匹配,但他会找到一个字符串是否存在
给定行中的
。 ;-)

-

boa


libclc home: http://libclc.sourceforge.net



Maybe he can ignore the \n and just use strstr() instead? He won''t get
exact matches for the whole line, but he will "find if a string exists
in a given line". ;-)
--
boa

libclc home: http://libclc.sourceforge.net


这篇关于strcmp但是'\ n'作为终止者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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