Thread: printing a string between indicies (data between xml tags)

  1. #1
    Registered User
    Join Date
    Apr 2004

    Question printing a string between indicies (data between xml tags)

    hi everyone,

    i seem to be having a simple printing error...which relates to simply printing between two indicies of a string (ie. char)

    eg. if i give the indices 1 and the string helloWorld - it should print hell my code, i am reading in a file line by line from the internet using the program wget (through a pipe) in unix...and searching it using the regex.h library and regular expressions...BTW you dont need to understand any of this to help me

    the file im reading in is just a simple xml
    <title> monsters rule </title>

    i then do a simple search for all of that title, including the title returns me with the indicies of the first < of title and ending > of /title

    so in this case it would return me start index=1 and end index=27

    now all i want to do is store the data between the title tags in a i cant print it...this is how i do it....please tell me if you know a better way!

    i calculate the length of <title> and </title>
    and then i add the length of <title> to the start index
    and subtract the length of </title> from end index

    <title> monsters rule </title>

    so in this case above my start index will nolonger be on < (1), it will be on the space (8)

    similarly with the end index...

    now all i need to do is print each character of the string between the indices...i did this...and it does work for most cases...

    but in some instances it prints out some weird stuff

    ie. here is a tag im reading in...


    so i have this in a string and i apply what i said earlier to remove the link tags...therefore when i print between the tags, it should print

    instead i get>>▼

    heres a small block of my code where this is done..

    ive also attached my full code

    i would appreciate any regards to why it doesnt print what i want...please be specific cause ive spent hours trying to debug this thing and i cant find whats the problem

    thanks for reading this message / and hopefully helping me..
    Last edited by ccoder01; 04-16-2004 at 05:00 AM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    You are not null terminating your tagdata string.

    	// Change: Add space for a null terminator.
    	tagdata = (char *)calloc((end-start) + 1, sizeof(char));
    	while(start < end) {
    		tempChar = searchString[start];
    		tagdata[counter] = tempChar;
    	// Change: null terminate the tagdata string...
    	// Not actually needed since you are using calloc()
    	// which initializes all memory to zero but better to
    	// be safe than sorry.
    	tagdata[counter] = '\0';
    	// Now you can print tagdata safely...
    P.S. Consider using strncpy() instead of a loop.
    Last edited by anonytmouse; 04-16-2004 at 02:36 AM.

  3. #3
    Registered User
    Join Date
    Apr 2004
    thanks it worked!....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM