why it can not print whole sentence by fputs,fseek,fgets

Dear Advanced c/g++ programers:

I copied and tested a piece simple (Creating a temporary file) on my g++4.5.2
on ubuntu10.04(linuxKernel 2.6.35-25)
------------
// Example 10-13. Creating a temporary file
#include <iostream>
#include <cstdio>

using namespace std;

int main() {

FILE* pf = NULL;
char buf[256];

pf = tmpfile(); // Create and open a temp file

if (pf) {
fputs("This is a temp file", pf); // Write some data to it
}

fseek(pf, 5, SEEK_SET); // Reset the file position
fgets(buf, 255, pf); // Read a string from it
fclose(pf);

std::cout << buf << '\n';
}
----------------------------------------------------------
it can compile, but when it run, the result is a little out of expect
--------
eric@eric-laptop:~/cppcookbook/ch10$ ./a.out
is a temp file
---------
why it can not printout the whole sentence that fputs enter
"This is a temp file" but just "is a temp file" without "This"?

you can download that example's source code from
http://examples.oreilly.com/9780596007614/
to test by yourself

looking to see your help and thanks a lot in advance, Eric
That is because you are skipping the first five bytes of the file when you pass the 5 to fseek. In effect, you have bypassed the letters "This "!
Topic archived. No new replies allowed.