473,770 Members | 5,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcmp but with '\n' as the terrminator

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
Nov 13 '05 #1
53 8296
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 : bi****@eton.pow ernet.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
Nov 13 '05 #2

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:bf******** **@news.freedom 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
Nov 13 '05 #3
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

Nov 13 '05 #4
In 'comp.lang.c', "Allan Bruce" <al*****@TAKEAW AYf2s.com> 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- em**********@no os.fr [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/
Nov 13 '05 #5

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:bf******** **@news.freedom 2surf.net...

"code_wrong " <ta*@tac.ouch.c o.uk> wrote in message
news:bf******** **@newsg2.svr.p ol.co.uk...

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:bf******** **@news.freedom 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
Nov 13 '05 #6
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 : bi****@eton.pow ernet.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
Nov 13 '05 #7
Emmanuel Delahaye wrote:
In 'comp.lang.c', "Allan Bruce" <al*****@TAKEAW AYf2s.com> 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 : bi****@eton.pow ernet.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
Nov 13 '05 #8
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

Nov 13 '05 #9

"Bjørn Augestad" <bo*@metasystem s.no.spam.to.me > wrote in message
news:RW******** ************@ju liett.dax.net.. .
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
Nov 13 '05 #10

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

Similar topics

10
11321
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
34
2750
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is constant: i.e. five or the string is abc????? xyz How can i generalize it for any length of the string?
47
3027
by: fishpond | last post by:
One way I've seen strcmp(char *s1, char *s2) implemented is: return immediately if s1==s2 (equality of pointers); otherwise do the usual thing of searching through the memory at s1 and s2. Of course the reason for doing this is to save time in case equal pointers are passed to strcmp. But it seems to me that this could create an inconsistency in the degenerate case when s1 points to memory that is not null-terminated, i.e. by some freak...
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10002
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.