[Expat-discuss] re: Invalid Token: Carriage return?

Karl Waclawek karl at waclawek.net
Thu Sep 25 12:49:20 EDT 2003


> fgets (buff, sizeof (buff), fp);
> 
> while (!feof (fp)) {
> if (XML_Parse (parser, buff, sizeof (buff), feof (fp)) == 0) {
> dsyslog (LOG_ERR, "Parse error at line %d, character %d:\n%s\n",
> XML_GetCurrentLineNumber (parser), XML_GetCurrentColumnNumber (parser),
> XML_ErrorString (XML_GetErrorCode (parser)));
> retcode = -1;
> goto CLOSE_FILE;
> } /* if */
> 
> fgets (buff, sizeof (buff), fp);
> 
> } /* while */

Use fread, not fgets. We are not passing ASCII characters
to Expat, but bytes that could be encoded in various ways.
You have to look at the file as binary, not text.

It also seems that your "while (!feof (fp)) {...}" loop
prevents parsing if the first buffer read already
encounters the end of the file.

Then, pass the return value from fread instead of sizeof(buffer)
to XML_Parse, do not assume the buffer is full every time.

That's just from a visual inspection. Fred is more of a C programmer
than I am, so he may have better advice.

Karl



More information about the Expat-discuss mailing list