Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strcmp but with '\n' as the terrminator

Reply
Thread Tools

strcmp but with '\n' as the terrminator

 
 
Allan Bruce
Guest
Posts: n/a
 
      07-19-2003
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


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-19-2003
Allan Bruce wrote:

> 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?


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 : http://www.velocityreviews.com/forums/(E-Mail Removed)
"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
 
Reply With Quote
 
 
 
 
code_wrong
Guest
Posts: n/a
 
      07-19-2003

"Allan Bruce" <(E-Mail Removed)> wrote in message
news:bfbc7r$3ul$(E-Mail Removed)2surf.net...
> 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?


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


 
Reply With Quote
 
=?ISO-8859-1?Q?Bj=F8rn_Augestad?=
Guest
Posts: n/a
 
      07-19-2003
Richard Heathfield wrote:
> Allan Bruce wrote:
>
>
>>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?

>
>
> 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).
>


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

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-19-2003
In 'comp.lang.c', "Allan Bruce" <(E-Mail Removed)> wrote:

> 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?


The usual trick is to remove the '\n' from the read line:

#include <string.h>

<...>

{
char *p = strchr (line, '\n'); /* search ... */

if (p)
{
*p = 0; /* ... and kill. */
}
}

--
-ed- (E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
code_wrong
Guest
Posts: n/a
 
      07-19-2003

"Allan Bruce" <(E-Mail Removed)> wrote in message
news:bfbi8r$6i6$(E-Mail Removed)2surf.net...
>
> "code_wrong" <(E-Mail Removed)> wrote in message
> news:bfben5$iud$(E-Mail Removed)...
> >
> > "Allan Bruce" <(E-Mail Removed)> wrote in message
> > news:bfbc7r$3ul$(E-Mail Removed)2surf.net...
> > > 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?

> >
> > 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
> >
> >

>
> I have used strstr mainly, not strcmp as my post indicates! (doh)
> But the problem is still that strstr requires a null terminator and not
> '\n'.
> Any ideas?


Which function are you using to read your file?
If you read your file with fgets() then you will get null terminated strings
to play with.
Of course there is still the newline character to take into account, but
that will not matter if you use strstr() to check for substrings.

cw


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-19-2003
Bj[o]rn Augestad wrote:

> Richard Heathfield wrote: <all snipped>
>> Allan Bruce wrote:
>>
>>
>>>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?

>>

<snip>
>
> 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".


Well, he did say quite clearly that there was no '\0' at the end of the
data. Or did I misunderstand him?

--
Richard Heathfield : (E-Mail Removed)
"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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-19-2003
Emmanuel Delahaye wrote:

> In 'comp.lang.c', "Allan Bruce" <(E-Mail Removed)> wrote:
>
>> 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?

>
> The usual trick is to remove the '\n' from the read line:
>
> #include <string.h>
>
> <...>
>
> {
> char *p = strchr (line, '\n'); /* search ... */


Undefined behaviour if line has no terminating null character, as the OP has
pointed out twice now.

<snip>

--
Richard Heathfield : (E-Mail Removed)
"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
 
Reply With Quote
 
=?ISO-8859-1?Q?Bj=F8rn_Augestad?=
Guest
Posts: n/a
 
      07-19-2003
Richard Heathfield wrote:

> Bj[o]rn Augestad wrote:
>
>
>>Richard Heathfield wrote: <all snipped>
>>
>>>Allan Bruce wrote:
>>>
>>>
>>>
>>>>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?
>>>

> <snip>
>
>>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".

>
>
> Well, he did say quite clearly that there was no '\0' at the end of the
> data. Or did I misunderstand him?
>


I don't know.

I was just assuming(I know, I know...) that the OP was reading a file
line by line using fgets() and then tried to match some string with the
line read, but ran into problems because of the trailing \n.

Only time and some source code will tell.

--
boa

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

 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      07-19-2003

"Bjørn Augestad" <(E-Mail Removed)> wrote in message
news:RWdSa.3690$(E-Mail Removed)...
> Richard Heathfield wrote:
>
> > Bj[o]rn Augestad wrote:
> >
> >
> >>Richard Heathfield wrote: <all snipped>
> >>
> >>>Allan Bruce wrote:
> >>>
> >>>
> >>>
> >>>>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?
> >>>

> > <snip>
> >
> >>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".

> >
> >
> > Well, he did say quite clearly that there was no '\0' at the end of the
> > data. Or did I misunderstand him?
> >

>
> I don't know.
>
> I was just assuming(I know, I know...) that the OP was reading a file
> line by line using fgets() and then tried to match some string with the
> line read, but ran into problems because of the trailing \n.
>
> Only time and some source code will tell.
>
> --
> boa
>
> libclc home: http://libclc.sourceforge.net
>


I am using this to read the file:
// find how big the file is
fseek(fptr, 0, SEEK_END);
size = ftell(fptr);

//allocate memory for string
if ( (contents = new char[size]) == NULL)
return 0;

Basically reading it in one big chunk, since I am doing some things to the
code that take a long time so I wanted to keep the file open for as little
time as possible.
I use strstr() to find some matches and also use strcmp() to see if some are
true for example, a line may be:
# Material: Porsche_Body
Now this will be stored with a '\n' at the end but no '\0'.
In this example I wish to search for "Material:" using strstr() but if it
doesnt exist then strstr() is causing undefined behaviour. If strstr() is
successful, then I want to see if the material name matches what I already
have loaded using strcmp() but since the '\0' isnt there - problems. I
count how many chars until the '\n' and then use strncmp I suppose, but that
doesnt get around the strstr() and I want to know for future how to use
strcmp with '\n' terminator.
From the gist of it, I should program my own function, or better still
macro.
Am I correct?
Thanks
Allan


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strcmp() And if Eirik C Programming 11 12-17-2003 03:19 AM
segfault when strcmp Robert Mens C Programming 6 10-23-2003 02:53 AM
strcmp muser C++ 6 10-09-2003 08:18 AM
strcmp problem Shane Peck C++ 6 09-22-2003 05:44 PM
please help with strcmp() Andrej Hocevar C Programming 3 07-19-2003 04:41 PM



Advertisments