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 
xmlns=\"http://www.w3.org/1999/xhtml\";>\n<head>\n<title>test</title>\n</head>\n<body>\n<p>test</p>\n</body>\n</html>\n";
        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");
                exit(EXIT_FAILURE);
        }
        xmlFreeDoc(doc);
        xmlCleanupParser();
        exit(EXIT_SUCCESS);
}
---

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="http://www.w3.org/1999/xhtml";>
<head>
<title>test</title>
</head>
<body>
<p>test</p>
</body>
</html>
---

However, with this solution, I loose the 'options' parameter that was available with the 'xmlSaveToFd' 
function.
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.

Nick


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