Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does fgetpos() return a negative offset?
    primarykey
    data
    text
    <p>When I am using <code>fgetpos(fp,&amp;pos)</code>, the call is setting <code>pos</code> to a negative value where <code>pos</code> is of type <code>fpos_t</code>. Can some one explain why this is happening?</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;stdlib.h&gt; #define TRUE 1 #define FALSE 0 #define MAX_TAG_LEN 50 char filename[1000] = "d:\\ire\\a.xml"; //extract each tag from the xml file int getTag(char * tag, FILE *fp) { //skip until a beginning of a next while(!feof(fp)) if((char)fgetc(fp) == '&lt;')break; if(!feof(fp)){ char temp[MAX_TAG_LEN]={0}; char *ptr; int len; fpos_t b; fgetpos(fp,&amp;b); // here the b is containing -ve values.....??? fread(temp,sizeof(char),MAX_TAG_LEN - 1,fp); temp[MAX_TAG_LEN-1] = 0; ptr = strchr(temp,'&gt;'); //search of ending tag bracket len = ptr - temp + 1; sprintf(tag,"&lt;%.*s",len,temp); //copy the tag printf("%s",tag); //print the tag b += len; //reset the position of file pointer to just after the tag character. fsetpos(fp,&amp;b); return TRUE; } else{ return FALSE; } } int main() { int ch; char tag[100]={0}; FILE *fp = fopen(filename,"r"); while(getTag(tag,fp)){ } fclose(fp); return 0; } </code></pre> <p>where a.xml is a very basic xml file</p> <pre><code>&lt;file&gt; &lt;page&gt; &lt;title&gt;AccessibleComputing&lt;/title&gt; &lt;id&gt;10&lt;/id&gt; &lt;redirect /&gt; &lt;revision&gt; &lt;id&gt;133452289&lt;/id&gt; &lt;timestamp&gt;2007-05-25T17:12:12Z&lt;/timestamp&gt; &lt;contributor&gt; &lt;username&gt;Gurch&lt;/username&gt; &lt;id&gt;241822&lt;/id&gt; &lt;/contributor&gt; &lt;minor /&gt; &lt;comment&gt;Revert edit(s) by [[Special:Contributions/Ngaiklin|Ngaiklin]] to last version by [[Special:Contributions/Rory096|Rory096]]&lt;/comment&gt; &lt;text xml:space="preserve"&gt;#REDIRECT [[Computer accessibility]] {{R from CamelCase}}&lt;/text&gt; &lt;/revision&gt; &lt;/page&gt; &lt;/file&gt; </code></pre> <p>The code is working for some xml files but for the above xml file it is stoping after printing the first tag.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload