#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    C tag-removing script


    Hello.

    I am trying to make a script in c that will search through a text file with lines like this (TR=009;dir=i;day=m-f;TI=13:05;stn=south;Line=framingham). The individual fields in each line are separated by semicolons. What I want to do is make the script remove the "tags" (i.e. TR=, dir=, day=, etc) but keep the semicolons and the info in front of the tags (i.e. the first line would be 009;i;m-f;13:05;south;framingham). Here's the code I have so far (btw i'm programming in a unix environment so i'm just running the script and funneling the text file into it):

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

    int main()
    {
    int c;
    char charArray[500];
    int i = 0;
    int k = 0;
    int tempi = 0;

    fgets(charArray, 500, stdin);



    while (charArray[i])
    {
    if (charArray[i] == ';')
    {
    while (charArray[i] != '=')
    {
    i++;
    charArray[i] = '\0';
    }
    charArray[i] = '\0';
    }

    else
    {
    i++;
    }

    }

    i = 0;

    while (charArray[i])
    {
    putchar(charArray[i]);
    i++;
    }

    return 0;
    }

    and after that i just type in: ./rmtags < sched
    (rmtags is the script, sched is the text file)

    For some reason when i run this, nothing happens. Could you please help me with my code? If not, can u suggest a new method/code i could use to do the same thing? Thx in advance

    Comments on this post

    • DaWei_M disagrees : For being too selfish/self-centered to read the stickies, FAQs, and rules. Just sayin'.
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,809
    Rep Power
    1574
    first things first: let me put your code inside of [CODE] tags to make it more readable:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	int c;
    	char charArray[500];
    	int i = 0;
    	int k = 0;
    	int tempi = 0;
    
    	fgets(charArray, 500, stdin);
    	
    
    
    	while (charArray[i])
    	{
    		if (charArray[i] == ';')
    		{
    			while (charArray[i] != '=')
    			{	
    				i++;	
    				charArray[i] = '\0';
    			}	
    			charArray[i] = '\0';
    		}	
    		
    		else 
    		{
    			i++;
    		}
    				
    	}	
    
    	i = 0;
    
    	while (charArray[i])
    	{
    		putchar(charArray[i]);
    		i++;
    	}
    	
    return 0;
    }
    This is important here, as the default behavior of the forum software is to ignore extra spaces meaning that the indentation is lost. In the future, you should always remember to use code or highlight tags around your code.

    As for the code itself, there are a number of things that catch my eye. First off, you define c, k and tempi but don't use them; this won't affect the program behavior, but it does clutter things a little. More importantly, I'm surprised that the compiler doesn't give a warning about unused variables; this tells me that you probably need to set the compiler warning option to maximum in order to get more feedback about the code.

    The heart of the problem, however, is that you are using the value of chararray[i] as the test of the loops. This (and the use of the term 'script' to describe the program) indicates a lack of familiarity with C, and specifically with how C strings work. You presumably expected that the test would only be false at the end of the array, but C arrays are not bounded in this fashion - it is entirely possible to overrun the end of the array and keep going.

    A change that may work for this purpose would be something like the following:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char charArray[500];
        int i = 0;
        int k = 0;
    
    
        fgets(charArray, 500, stdin);
    
        k = strlen(charArray);
    
        for (i = 0; i < k; i++)
        {
            if (charArray[i] == ';')
            {
                putchar(charArray[i]);
                i++;
                for (; charArray[i] != '='; i++)
                {
                    /* skip these characters */
                }
            }
            else
            {
                putchar(charArray[i]);
            }
        }
    
    return 0;
    }
    It's still not an ideal solution, but it (almost) does what you need.
    Last edited by Schol-R-LEA; September 18th, 2010 at 07:16 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    Ok, that doesn't help me. I know the variables are unused (they are there because of a previous version of the script and i forgot to delete them). I am using the cc compiler preinstalled on most unix systems and it is very basic. Can you please help answer my question?
  6. #4
  7. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,809
    Rep Power
    1574
    Sorry, I was still editing my reply; you should be able to see what I added now.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    I tried using your code, but, like I said, I'm programming in c. I cant use the strlen() function you described here. Is there any other method?
  10. #6
  11. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,809
    Rep Power
    1574
    What do you mean? the strlen() function is a standard C function, defined in string.h, and should be usable from any standard compiler and library. Is there some other reason you can't use it?

    As for the warning messages, chances are the compiler you're using is GCC aliased to cc; try using the option -Wall on the command line.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    Sorry, I just realized that. For some reason it wasn't compiling it before. Thank you for your help (it mostly works now). The only problem with the code is that if I try to input the whole sched text file, it only removes the tags from one line and then finishes. For some reason it won't loop through the entire text file (keep in mind the file is over 8000 lines long so i don't know if a char array will hold the whole thing). Is there a way to populate the array with each line of the text file, output it without the tags, then clear the array and fill it with the next line, out put it, and so on? Thx in advance!
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,489
    Rep Power
    1876
    > fgets(charArray, 500, stdin);
    This reads one line

    while ( fgets(charArray, 500, stdin) != NULL )
    This reads the whole file, one line at a time.

    Just add { } around the code you want to apply to every line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    146
    Rep Power
    0
    Originally Posted by Schol-R-LEA
    first things first: let me put your code inside of [CODE]
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char charArray[500];
        int i = 0;
        int k = 0;
    
    
        fgets(charArray, 500, stdin);
    
        k = strlen(charArray);
    
        for (i = 0; i < k; i++)
        {
            if (charArray[i] == ';')
            {
                putchar(charArray[i]);
                i++;
                for (; charArray[i] != '='; i++)
                {
                    /* skip these characters */
                }
            }
            else
            {
                putchar(charArray[i]);
            }
        }
    
    return 0;
    }
    It's still not an ideal solution, but it (almost) does what you need.
    sorry but the last code doesn't remove the frist part of the string he wants to remove, i think it's better to skip also that part
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char charArray[500];
        int i = 0;
        int k = 0;
    
    
        fgets(charArray, 500, stdin);
    
        k = strlen(charArray);
    	
        while(charArray[i++] != '='); // skip the frist part
    
        for (; i < k; i++)
        {
            if (charArray[i] == ';')
            {
                putchar(charArray[i]);
                i++;
                for (; charArray[i] != '='; i++)
                {
                    /* skip these characters */
                }
            }
            else
            {
                putchar(charArray[i]);
            }
        }
    
    return 0;
    }

    Comments on this post

    • Schol-R-LEA agrees : That's why I said 'almost'. I figured the OP would finish the job.

IMN logo majestic logo threadwatch logo seochat tools logo