Re: [xml] xmlSaveToFd: unexpected behavior while redirecting the 'stdout' stream to a file

Thanks for the explanation.

In order to use buffered IO only, I am now using the 'xmlSaveFile' function (instead of 'xmlSaveToFd'):
#include <libxml/tree.h> /* for xmlSaveFile */
#include <stdio.h> /* for printf */
#include <string.h> /* for strlen */
int main()
        const char *str = "<html 
        xmlDocPtr doc = NULL;
        printf("Content-Type: application/xhtml+xml;\n\n");
        doc = xmlParseMemory(str, strlen(str) + 1);
        if(xmlSaveFile("-", doc) == -1)
                fprintf(stderr, "Unable to dump the document to a file.\n");

Now, I have the expected behavior while redirecting the 'stdout' stream to a file:
$ ./main.out 1>1.txt
$ cat 1.txt
Content-Type: application/xhtml+xml;

<?xml version="1.0"?>
<html xmlns="";>

However, with this solution, I loose the 'options' parameter that was available with the 'xmlSaveToFd' 
Is there a similar solution with the 'options' parameter please?

On Tue, 3 Apr 2018 14:28:24 +0200
Nick Wellnhofer <wellnhofer aevum de> wrote:

On 03/04/2018 14:02, YuGiOhJCJ Mailing-List via xml wrote:
    printf("Content-Type: application/xhtml+xml;\n\n");

This operates on the stdout FILE pointer using buffered IO.

    ctxt = xmlSaveToFd(1, NULL, XML_SAVE_FORMAT);

This operates on the stdout file descriptor using unbuffered IO. You're mixing 
buffered and unbuffered IO.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]