?
Solved

How does "strtok()" search in a given character string line?

Posted on 2003-03-27
3
Medium Priority
?
1,081 Views
Last Modified: 2008-02-01
Hello Everyone!  

I have a following up question to the same code which is posted about a week ago.  My ex-neighbor wrote the following C-Program to organize and chop a big ASCII text down into a convenient set of files several years ago.   Now, I want to understand the Code completely by myself.   I mostly understand the vocabularies such as *inf, fopen, feof, and strncmp as well as the "!" symbol.   This time, my question is how to use "strntok()" and how "strntok()" works when it is scanning in a incoming character string.  The code have two occasions.   Could someone explain these codes in comprehensive words?  I also post the part of Data (after the CODE, all way down), so that you may see the code clearer.

              t2=strtok(tcopy,"-");
              t2=strtok(NULL," ");
               
Mitsuru (in Los Angeles)

======== THE CODE FOLLOWS =======================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_LEN 1024

void main(int argc, char *argv[])
{
    FILE *inf,*outf;
    char text[BUF_LEN], tcopy[BUF_LEN], *t2;

    if (argc<2) {
         printf("Need a filename!\n");
         exit(1);
    }
    if (!(inf=fopen(argv[1],"rt"))) {
         printf("Could not open input file [%s]\n",argv[1]);
         exit(1);
    }
    while (!feof(inf)) {
         fgets(text,BUF_LEN-1,inf);
         if (!strncmp(text,"UI",2)) {
              strncpy(tcopy,text,BUF_LEN-1);
              t2=strtok(tcopy,"-");
              t2=strtok(NULL," ");
              t2[strlen(t2)-1]=0;
              printf("found UI : %s\n",t2);
              t2=strcat(t2,".txt");
              fclose(outf);
              if(!(outf=fopen(t2,"wt"))) {
                   printf("Could not open output file: %s\n",t2);
                   exit(1);
              }
         }
         fputs(text,outf);
    }
    fclose(inf);
    fclose(outf);
}

========A part sample of DATA FILE follows.==========

UI - Abreu-CT

Francis L. Abreu Charitable Trust
c/o SunTrust Banks, Inc.  
P.O. Box 4655, MC 221  
Atlanta, GA  30302-4655
Contact: Danah Craft, Asst. V.P., SunTrust Banks, Inc.  
URL: http://www.abreufoundation.org 
EIN: 586130937


============================================================
UI - AEC-Tr

The AEC Trust
50 Hurt Plz., Ste. 900  
Atlanta, GA  30303
Contact: Christine Tryba-Cofrin
FAX: (404) 577-9490
Donor(s):  Members of the Cofrin family.
EIN: 366725987


=============================================================
UI - Alberto

Alberto-Culver Company Contributions Program
2525 Armitage Ave.  
Melrose Park, IL  60160-1163
Telephone: (708) 450-3000
Contact: Nancy Shields, Mgr., Corp. Comm.  
FAX: (708) 450-3354


0
Comment
Question by:mkido
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
gj62 earned 200 total points
ID: 8221396
strtok finds tokens that are separated by a specific character, or one of a set of characters (often referred to as delimiters).  

Take for example the following line of first names:

char names[] = "glenn;robert;george;francis";
char *name1,*name2,*name3,*name4;

If you wanted to parse these names into separate strings, you would do:

name1 = strtok(names,";"); /*return the FIRST string up to the first semicolon (delimiter), or end of string.  Do not return the first delimiter*/

name2 = strtok(NULL, ";"); /*parse the rest*/
name3 = strtok(NULL, ";");
name4 = strtok(NULL, ";");

Notice you don't repeat names, rather you use NULL, and strtok "remembers" the string you were working on (there's problems here in threaded apps, but let's not worry about that right now).  Strtok allows you to have more than one delimited, I could do the following:

char names[] = "glenn robert,george;francis";
char *name1,*name2,*name3,*name4;


name1 = strtok(names,"; ,"); /*now we can use a comma, a space, OR a semicolon as a delimiter*/
name2 = strtok(NULL, ";"); /*parse the rest*/
name3 = strtok(NULL, ";");
name4 = strtok(NULL, ";");

You can also change the delimiter on subsequent calls to be whatever you wish, as your program above does.  This can be done successfully only when the file follows a rigid layout structure.

Lastly, strtok works by actually modifying the string it works on.  What it returns is a pointer to the *beginning* of the next token, and replaces the delimiter with NULL.

Originally, my string looked like:

glenn;robert;george;francis0

if you said

printf("%s", names), it would print out just like above...

When strtok got through with it, it looked like:

glenn0robert0george0francis0

with each 0 being null (which is how C knows it has come to the end of the string).  If you now said:

printf("%s", names), it would print:

glenn

that's it - because there is now a null there, and that's where C thinks the string stops.  But name1 points to the "r" in robert, so you could printf name1, and it would print

robert

hope this helps...
0
 

Author Comment

by:mkido
ID: 8229344
Thank you, qj62.   I will test a few variations from your explanation, and see how srntok() works.  One moment.
0
 

Author Comment

by:mkido
ID: 8239338
Dear gj62:

Your answer is so gorgeous.  I saw everything you talked about.  Thank you.  mkido
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question