strok

This is a discussion on strok within the C Programming forums, part of the General Programming Boards category; Can anyone please tell me why my program will not compile I'm trying to read a file into my program ...

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

    strok

    Can anyone please tell me why my program will not compile

    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.

    What I'm trying to do is create an email address something like D.ABBOTTCMU96143@uk.ac.tees.scorch

    This is what I have got so far I think the problem lies with the function decleration but not sure, can anyone please help.

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

    char *make(char *s);

    int main()

    { char buffer[101], s[101] , *name[20] ;
    char *mark, *test;
    int i;
    FILE *ifp, *ofp;

    if ((ifp=fopen("scmemail.txt", "r")) == NULL)
    printf("could't open scmemail.txt");
    if ((ofp=fopen("email.txt", "w")) == NULL)
    printf("couldn't open email.txt");

    while(fgets(buffer,101,ifp) !=NULL)

    {
    strcpy(s,buffer);
    switch(buffer[0])
    {
    case'#':break;
    case'&'rintf("here now");
    test = make(s);
    break;
    }
    char *make(char *s)
    {
    i==0;
    mark=strtok(s," &");

    while(*mark != '-')
    {
    name[i++]=mark;
    mark=strtok(NULL, " ");
    printf("got this far");
    }

    return s;
    }
    fputs(mark,stdout);

    /* fputs(mark,stdout);*/
    /* fputs(buffer,stdout);/*
    /* fputs(buffer,ofp); */

    }

    fclose(ifp);
    fclose(ofp);


    Thank you for any advise, I am new to C If anyone knows what I need to do would you explain to me, I want to be able to learn rather than just copy.

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    It looks like you are trying to define the function make() within the function main().

    Code:
    while(fgets(buffer,101,ifp) !=NULL)
    {
      strcpy(s,buffer);
      switch(buffer[0])
      {
        case'#':break;
        case'&':printf("here now");
        test = make(s);
        break;
      }
      char *make(char *s)
      {
        i==0;
        mark=strtok(s," &");
     
        while(*mark != '-')
        {
          name[i++]=mark;
          mark=strtok(NULL, " ");
          printf("got this far");
        }
    make()'s definition has been placed within a while loop of main(), which is no good.
    Jason Deckard

Popular pages Recent additions subscribe to a feed

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