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 08-29-2007, 11:45 AM   #1
trevorv
LQ Newbie
 
Registered: Jan 2005
Location: Isle of Wight, England
Distribution: Ubuntu
Posts: 8

Rep: Reputation: 0
C: Extracting part of a string


After a couple of weeks break due to other commitments, I've started learning C again. I'm writing a simple address book program. In order to know what types of data to ask for the program reads a config file containing the data, e.g.

Code:
field.firstname "First name"
field.surname "Surname"
field.jabber "Jabber ID"
I have written the part which reads this file, it takes a line at a time. I cannot figure out, however, how I can extract the part of the newly read string between quotation marks.

In other words, I want to give the part of each line in quotation marks to printf, but I don't know how to separate it from the rest.

Help appreciated :-)

tsv
 
Old 08-29-2007, 12:41 PM   #2
rjwilmsi
Member
 
Registered: Mar 2005
Location: UK
Distribution: opensuse 12.2 x86_64
Posts: 563

Rep: Reputation: 38
One way would be to do a character by character compare on the whole string and cat characters after the first " and before the second " into a new string:

Code:
for (i = 0; i < strlen(inputstring); i++)
{

if (start_cat == 0  && inputstring[i] == '"') // start of string
  start_cat = 1;
else if (inputstring[i] == '"') // end of string
   start_cat = 0 ;
else if (start_cat == 1) // in the string
 strcat(outputstring, inputstring[i]);

}
probably ugly but something like that will work
 
Old 08-29-2007, 03:17 PM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Not tested:

Code:
ptr1=strchr(thestring,"\""); // find first double quote
ptr1++;
ptr2=strchrt(ptr1,"\""); // find second double quote
*ptr2='\0';
printf("%s\n",ptr1);
*ptr2="\""; // restore original value (if necessary)
Not included is error checking if the quotes are actually found.

Last edited by Wim Sturkenboom; 08-29-2007 at 03:19 PM.
 
Old 08-29-2007, 04:36 PM   #4
henk1420
LQ Newbie
 
Registered: Aug 2007
Location: Europe
Distribution: Debian
Posts: 5

Rep: Reputation: 0
strtok

G'day,

You may use the function strtok.

char *token_, *your_string;

if ( (token_ = strtok(your_string, "\"\r\n")) == NULL)
{
// do some error messages and return
}

// token_ now contains the first part: with the . inside

if ( (token_ = strtok(NULL, "\"\r\n")) == NULL)
{
// do some error messages and return
}

// token_ points to the string between the double quotation marks
// just where you were looking for.


In stead of using strtok, which mangles your_string, you can use strsep instead. It does not mangle your string.

Both functions require to include <string.h>

Regards,
H
 
  


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
String extracting / string operation Xeratul Linux - General 24 02-13-2007 02:54 PM
Extracting multi-part RAR archives? Kixdemp Linux - Software 2 03-22-2006 05:44 PM
Print a part of variable/string p0tw0r Linux - Newbie 1 04-07-2005 02:49 PM
extracting first n characters of a string in perl ananthbv Programming 4 09-14-2004 07:25 AM
extracting more than one value from a string ganninu Programming 16 12-10-2003 03:26 AM

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

All times are GMT -5. The time now is 03:48 PM.

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