LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2014, 11:45 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
A question about \r\n in C...


I have a string that has several carriage returns line feeds in it. Of course a string ends in C when it is NULL terminated.

The pointer to the string I am working with is a const char *. I need to pull out the data between each \r\n. I can't use strtok() because the pointer is a const and strtok changes the string. I don't want to write a function that checks each byte for the \r\n sequence. Are there any other functions that would do this?

-Tristan
 
Old 09-27-2014, 11:51 PM   #2
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
I suppose I can make a non const copy of the string and use strtok on that. This is for a packet analyzer of web traffic so I want it to be as efficient as possible. It just seems so expensive to do this on each HTTP request.

-Tristan
 
Old 09-28-2014, 03:00 AM   #3
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
You could also try strcspn(). http://www.cplusplus.com/reference/cstring/strcspn/
 
1 members found this post helpful.
Old 09-28-2014, 08:07 AM   #4
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Great thank you. I will try that.

-Tristan
 
Old 10-10-2014, 04:46 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Writing a function to perform the check is quite trivial.

All you need to do is literate through the string and check for the first character e.g. "\r"
and if it matches then check the current position+1 and see if it matches the second character e.g. "\n"
and if that matches return the position. If there is no match, then increment and try again until hitting \0

If you need to find multiple matches in a string you can either add them to a list stored in an array
or you can have your search function take an argument as to where to start in the string.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] GPL question (Version 2, June 1991) - physical media availability question LicenseQuestions Linux - Newbie 1 12-01-2012 06:34 PM
basic html question - download link to files on my webpage question Davno Linux - Server 5 12-25-2009 07:24 AM
linux distro question & mysql install question natalie.aloi Linux - Newbie 5 07-19-2009 08:28 PM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration