Printing certain fields 
Author Message
 Printing certain fields

Quote:

> How do I print certain fields in a string that has a delimiting character
> eg.

> string = "alan,555-7890,37 years old,....

> and I want to print the 2nd record, in this case a phone number.

Look into strtok(char * ,char const *);
it will do that, and so much more.

In your case, grab it like this:
 (  assume char buffer[100] has the input line )
char *ptr;
ptr = strotk(buffer,",");
ptr = strtok(NULL,",");  /* note NULL in agument indicates
                            continuation of same string  */

then print is out with the ptr:
printf("%s",ptr);

-Scotty



Fri, 12 May 2000 03:00:00 GMT  
 Printing certain fields

How do I print certain fields in a string that has a delimiting character
eg.

string = "alan,555-7890,37 years old,....

and I want to print the 2nd record, in this case a phone number.



Sat, 13 May 2000 03:00:00 GMT  
 Printing certain fields



Quote:
> How do I print certain fields in a string that has a

delimiting character

Quote:
> eg.

> string = "alan,555-7890,37 years old,....

> and I want to print the 2nd record, in this case a phone
number.

I would suggest you look at the standard C library functions
strchr() and strncpy(), both declared in string.h.

Jack



Sat, 13 May 2000 03:00:00 GMT  
 Printing certain fields

On Mon, 24 Nov 1997 23:21:49 -0600, "Spaceman Spiff!"

Quote:


>> How do I print certain fields in a string that has a delimiting character
>> eg.

>> string = "alan,555-7890,37 years old,....

>> and I want to print the 2nd record, in this case a phone number.

>Look into strtok(char * ,char const *);
>it will do that, and so much more.

Beware that if you have lines like:

abc,def,,jkl

with empty fields [optional, default values, etc.], strtok() is
useless.  In that case you'll have to roll your own using strchr() or
some such.

Beware also that strtok() is destructive and not re-entrant.

[note new email address]
==
Miguel Carrasquer Vidal                     ~ ~
Amsterdam                   _____________  ~ ~

========================== Ce .sig n'est pas une .cig



Sat, 13 May 2000 03:00:00 GMT  
 Printing certain fields

Quote:
> How do I print certain fields in a string that has a delimiting character
> eg.

> string = "alan,555-7890,37 years old,....

> and I want to print the 2nd record, in this case a phone number.

given:char string[] = "alan,555-7890,37 years old";
char sub1[5], sub2[9], sub3[13];

then calling:
sscanf(string, "%[^,]%[^,]%[^,]", sub1, sub2, sub3);

would result in:
sub1 = "alan\0", sub2 = "555-7890\0" and sub3 = "37 years old\0"

HTHs

~d



Sat, 13 May 2000 03:00:00 GMT  
 Printing certain fields

Quote:

> How do I print certain fields in a string that has a delimiting character
> eg.

> string = "alan,555-7890,37 years old,....

> and I want to print the 2nd record, in this case a phone number.

Hi Alan Claughan,

To do this in C you will have to take the string apart and extract
or transform the information you want to print into a substring.
Generally this can be done by using the functions from <string.h>,
as Jack and Scotty explain in their replies.

There is another (not very nice) way to do what you want, but you need
exact position and size information for this:

  char string[] = "alan,555-7890,37 years old,....";
  printf( "%.8s\n", string + 5 );

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 14 May 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Creating a new field in an exiting table at a certain position

2. printing certain months

3. Visual Studio .NET hangs with certain file and certain project settings

4. printing double in n character field

5. Formatted print with variable output field width.

6. Printing Time Field using Crystal Reports

7. Printing Problems (how to print, pause printing, then print more)

8. need to compare to structs, field by field, audit trails

9. Size of text-fields/edit-fields depending on Font selected

10. Syntax for a query copying records from field A to field B, VC++

11. Add fields with {\field in a richedit control

12. SMTP Sink using C# (getting envelope fields (ADODB.Fields))

 

 
Powered by phpBB® Forum Software