Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7966687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:29:47+00:00 2026-06-04T06:29:47+00:00

I’m having a problem when using the function vsprintf. I have 3 functions to

  • 0

I’m having a problem when using the function vsprintf.

I have 3 functions to open, close and write to a XML file. The open function stores the first word of the input text in an array, and the close function closes the tag with that word. The problem is that the array where I store the close tag to use gets overwritten in each call to the open or the write functions (even when the write function hasn’t any reference to the array used to store the close tag).

int xml_level = 0;
char *xml_header[64];
FILE *xml_out;

void xmlopen(const char *format, ...){
   char buffer[256];
   va_list arglist;
   va_start(arglist,format);
   vsprintf(buffer,format,arglist);
   va_end(arglist);
   int i;
   for(i=0; i<xml_level; i++){
      fprintf(xml_out,"\t");  
   }
   fprintf(xml_out,"<%s>\n",buffer);
   xml_header[xml_level] = strtok (buffer, " ");
   xml_level++;
}

void xmlclose(){
   xml_level--;
   int i;
   for(i=0; i<xml_level; i++){
      fprintf(xml_out,"\t");  
   }
   fprintf(xml_out,"</%s>\n",xml_header[xml_level]);
}

void xmlwrite(const char *format, ...){
   char buffer[256];
   va_list arglist;
   va_start(arglist,format);
   vsprintf(buffer,format,arglist);
   va_end(arglist);
   int i;
   for(i=0; i<xml_level; i++){
      fprintf(xml_out,"\t");  
   }
   fprintf(xml_out,"<%s/>\n",buffer);
}

Example of use:

xmlopen("Hello Word");
xmlopen("Foo Bar");
xmlwrite("Potato");
xmlwrite("Sentence longer than the other ones");
xmlclose();
xmlclose();

Example of output:

<Hello Word>
        <Foo Bar>
                <Potato/>
                <Sentence longer than the other ones/>
        </Sentence longer than the>
</Sentence longer than the>

Where it should be:

<Hello Word>
        <Foo Bar>
                <Potato/>
            <Sentence longer than the other ones/>
        </Foo>
</Hello>

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:29:48+00:00Added an answer on June 4, 2026 at 6:29 am

    Solution

    Change this line:

    xml_header[xml_level] = strtok (buffer, " ");
    

    to

    xml_header[xml_level] = strdup (strtok(buffer, " "));
    

    And remember to free xml_headers when your program exit.

    [UPDATE]
    Of course, you also need to check possible corner cases such as strtok returns NULL, etc…

    Explanation

    strtok won’t allocate extra storage for the returned tokens. My personal suspect is that it will subsititute the delimiters with \0 in place and return you the pointer to the start of next token every time.

    Note that in your code, you allocate 256 bytes of buffer both in xmlopen and xmlwrite at the beginning, and recall that this buffer will be allocated in stack. So in your calls to xmlopen or xmlwrite, buffer will actually point to the same address (you can print its value to verify this printf("buffer is %p\n", buffer), it’s 0xbff2481c on my machine).

    First, you call xmlopen("Hello World"), and xml_header[0] will point to "Hello", which is also the start of buffer. Then you call xmlopen("Foo Bar"), and xml_header[1] will point to "Foo", which also the start of buffer. Then you call xmlwrite("Portato") and xmlwrite("Sentence longer than the other ones"). Note that at this point, xml_header[1] still point to the start of buffer, which now is "Sentence longer than the other ones". So when you call xmlclose(), it will print out that sentence instead of your expected token, which is overwrite by your later buffer.

    Interestingly, if you allocate different buffer size in xmlopen and xmlwrite, say 256 bytes for xmlopen and 128 bytes for xmlwrite, then you will see that xmlclose will print out some mess codes which is not readable.

    You can verify all these by inspect (e.g., print) the values of xml_header[0], xml_header[1] and buffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
I have a reasonable size flat file database of text documents mostly saved in
I have thousands of HTML files to process using Groovy/Java and I need to
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.