Simple Text Parsing.. Long Time Since College

This is a discussion on Simple Text Parsing.. Long Time Since College within the C Programming forums, part of the General Programming Boards category; Hello all, I was hoping someone wouldn't mind pointing me in the right direction. It's been a long while since ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    Simple Text Parsing.. Long Time Since College

    Hello all,
    I was hoping someone wouldn't mind pointing me in the right direction. It's been a long while since my last C programming class in college

    I'm looking to parse a text file.

    Looking like this:
    yellow 111111
    yellow 222222
    yellow 333333
    blue 111112
    blue 111113
    green 222221
    green 222223
    green 222224
    green 222225

    or this:
    111111 yellow
    222222 yellow
    333333 yellow
    111112 blue
    111113 blue
    222221 green
    222223 green
    222224 green
    222225 green


    basically what i need is to embed the numbers in the colors with open and close tags. the output should look like:

    #opencolor#yellow
    #number#111111
    #number#222222
    #number#333333
    #closecolor#yellow
    #opencolor#blue
    #number#111112
    #number#111113
    #closecolor#blue
    #opencolor#green
    #number#222221
    #number#222223
    #number#222224
    #number#222225
    #closecolor#green


    i'm just hoping someone can point me in a direction to start, or a suggestion on how to go about this.

    any help is appreciated.

    thanks in advance.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    itsme@itsme:~/C$ cat tags.c
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    int main(void)
    {
      FILE *fp;
      char buf[100], curcolor[100] = "";
      char *p;
      int num;
    
      if(!(fp = fopen("tags.txt", "r")))
      {
        perror("fopen()");
        exit(EXIT_FAILURE);
      }
    
      while(fgets(buf, sizeof(buf), fp))
      {
        if(*buf && buf[strlen(buf)-1] == '\n')
          buf[strlen(buf)-1] = '\0';
        num = strtol(buf, &p, 10);
        if(*p)
          p++;
        if(!strcmp(curcolor, p))
          printf("#number#%d\n", num);
        else
        {
          if(*curcolor)
            printf("#closecolor#%s\n", curcolor);
          strcpy(curcolor, p);
          printf("#opencolor#%s\n", curcolor);
          printf("#number#%d\n", num);
        }
      }
    
      fclose(fp);
    
      if(*curcolor)
        printf("#closecolor#%s\n", curcolor);
    
      return EXIT_SUCCESS;
    }
    itsme@itsme:~/C$ cat tags.txt
    111111 yellow
    222222 yellow
    333333 yellow
    111112 blue
    111113 blue
    222221 green
    222223 green
    222224 green
    222225 green
    itsme@itsme:~/C$ ./tags
    #opencolor#yellow
    #number#111111
    #number#222222
    #number#333333
    #closecolor#yellow
    #opencolor#blue
    #number#111112
    #number#111113
    #closecolor#blue
    #opencolor#green
    #number#222221
    #number#222223
    #number#222224
    #number#222225
    #closecolor#green
    itsme@itsme:~/C$
    Last edited by itsme86; 10-07-2004 at 11:10 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Wait, I changed my mind! I like this version better

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    int main(void)
    {
      FILE *fp;
      char buf[100], curcolor[100] = "";
      char *p;
    
      if(!(fp = fopen("tags.txt", "r")))
      {
        perror("fopen()");
        exit(EXIT_FAILURE);
      }
    
      while(fgets(buf, sizeof(buf), fp))
      {
        if(*buf && buf[strlen(buf)-1] == '\n')
          buf[strlen(buf)-1] = '\0';
    
        if((p = strchr(buf, ' ')))
          *p++ = '\0';
        else  // Ignore bad lines
          continue;
    
        if(!strcmp(curcolor, p))
          printf("#number#%s\n", buf);
        else
        {
          if(*curcolor)
            printf("#closecolor#%s\n", curcolor);
          strcpy(curcolor, p);
          printf("#opencolor#%s\n", curcolor);
          printf("#number#%s\n", buf);
        }
      }
    
      fclose(fp);
    
      if(*curcolor)
        printf("#closecolor#%s\n", curcolor);
    
      return EXIT_SUCCESS;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    bravo!

    thanks a million! i've edited it to accept a filename from the command line and enter a file name to write to. does this look correct?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    void main(int arcg, char *argv[])
    {
      FILE *infile, *outfile;
      char buf[100], curstr[100] = "";
      char *p;
      char file1[20] = " ";
      char file2[20] = " ";
    
       if (arcg != 3) {printf("STRIP [file to parse] [file to write to]\n");
       return;
       }
    
     strcpy(file1, argv[1]);
     strcpy(file2, argv[2]);
    
     if ((infile = fopen(file1,"r")) == NULL) { printf("The file that you want\n");  printf("scanned is not found.  Try again.");
                                  return;}
      if ((outfile  = fopen(file2,"w")) == NULL){ fclose(infile); return;}
    
    
      while(fgets(buf, sizeof(buf), infile))
      {
        if(*buf && buf[strlen(buf)-1] == '\n')
          buf[strlen(buf)-1] = '\0';
    
        if((p = strchr(buf, ' ')))
          *p++ = '\0';
        else  // Ignore bad lines
          continue;
    
        if(!strcmp(curstr, p))
          fprintf(outfile,"#TAP#%s\n", buf);
        else
        {
          if(*curstr)
            fprintf(outfile,"#CTO#%s\n", curstr);
          strcpy(curstr, p);
          fprintf(outfile,"#CTO#%s\n", curstr);
          fprintf(outfile,"#TAP#%s\n", buf);
        }
      }
    
      fclose(infile);
    
      if(*curstr)
        fprintf(outfile,"#CTC#%s\n", curstr);
    
    }

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Don't forget to fclose(outfile); also.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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