strtok

This is a discussion on strtok within the C Programming forums, part of the General Programming Boards category; Can anyone please help, I'm trying to read a file into my program and what I would like to do ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    6

    Unhappy strtok

    Can anyone please help, I'm trying to read a file into my program and what I would like to do is ignore any line starting with a # and the subsequent lines that start with & tokenise.

    for example an extract of the file I'm working with:

    # SCM Students
    &ABBOTT DARREN - 01 MSc COMP AIDED GRAPHICAL TECHNOLOGY APPLICATIONS
    M00641X CMG96001@uk.ac.tees.scorch
    M00641X CMG96001@uk.ac.tees.scm-arkengarthdale

    &ACHMET ALPER - 02 CERTIFICATE IN PROFESSIONAL DEVELOPMENT
    L36092X CMU96143@uk.ac.tees.scorch
    L36092X CMU96143@uk.ac.tees.scm-arkengarthdale


    What I want to be able to do is ignore the first line of the code then when we see & I would like to be able to tokenise ABBOTT and also DARREN.

    I am new to C programming and to be honest I'm going round in circles any help would be very very welcome because I'm loosing the will to live.

    At the moment all I've been able to do with any success is to get the file read in and to get the file printed out.

    This is what I have so far, pretty lame I know.

    #include<stdio.h>
    #include<string.h>

    int main()
    {
    char buffer [101];

    FILE *ifp,*ofp;

    ifp=fopen("scmemail.txt","r");
    ofp=fopen("email.txt","w");

    while (fgets(buffer,101,ifp) != NULL)
    {fputs(buffer,stdout);
    fputs(buffer,ofp);
    }
    fclose(ifp);
    fclose(ofp);

    return 0;
    }

    Thanks again for any help

    I'm using a compilier called Pacific C with non ansci c features disabled
    Last edited by ktntech; 01-13-2002 at 06:21 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > This is what I have so far, pretty lame I know.
    This is all we ask - that you try

    Your loop for reading each line from the file is fine - all you need to do is work on what happens to each line

    > I would like to do is ignore any line starting with a #
    Try this
    Code:
    if ( buffer[0] == '#' ) continue;   /* get the next line */
    > and the subsequent lines that start with & tokenise.
    Would be
    Code:
    if ( buffer[0] == '&' ) {
        char firstname[20], lastname[20];
        sscanf( buffer, "&%s %s", lastname, firstname );
        printf( "Found %s %s\n", firstname, lastname );  /* TEST CODE */
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    6
    Thank you for your help Salem, much much appreciated.



    Can you tell me what is the difference between this method and using the strtok command.

    Thanks again

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > Can you tell me what is the difference between this method and using the strtok command.
    strtok doesn't copy string tokens to other variables, it just points to the start of each token (in turn), and in the process, also modifies buffer.

    sscanf can extract several strings at once, and does not modify buffer.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    6

    strtok

    So does this mean that if I was to use strtok it would just point to what is tokenised and I would then have to pull that info out and assign it to another string ??

    I appologise for my ignorance.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    yes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21