|
From: Michael C. <jd...@ho...> - 2001-09-04 10:00:15
|
Hi Johannes,
I believe that fprintf() does move the file pointer it just looks as though
it isn't, try using ftell() to print the file position.
I believe that because there is a struct being written to the file via
fread() and then a string via fprintf() this is causing the problem as you
suggested.
These functions generally should never be mixed as the problems we are
seeing are caused. Also when reading files between different systems there
are problems concerning the endianess (big endian/little endian) for the
file, allignment, etc. For example, the allignment of an int on one machine
may be different for another. One would have to define a common alligned
struct for writing, similar to the way X11, sockets, and other network
protocols do it.
However, we are only using one os in this case so this shouldn't be a
problem.
I also used fgets() and feof() to test for EOF. The output is only "Hallo
World", and it doesn't see the number on the next line but reports EOF.
char line [256];
while (fgets (line, 256, fhandle) != NULL)
{
printf ("%s", line);
}
if (feof (fhandle))
{
printf ("EOF for fhandle.\n");
}
However, I have also used fscanf()with the field specifier and the
circumflex symbol to ignore upto the newline, etc. This works in certain
circumstances, i.e. only when I specify two scanf() commands as opposed to
one.
fseek (fhandle, 0L, SEEK_SET);
char c1[100];
char c2[100];
char c3[100];
char c4[100];
char c5[100];
/*
// doesn't work - only sees first line
printf ("pos %d\n", ftell (fhandle));
fscanf (fhandle, "%s%s%*[^\n]%*[^'O']%s%s%s", c1, c2, c3, c4, c5);
printf ("%s %s %s %s %s\n", c1, c2, c3, c4, c5);
printf ("pos %d\n", ftell (fhandle));
*/
// works well
printf ("pos %d\n", ftell (fhandle));
fscanf (fhandle, "%s%s%*[^\n]", c1, c2);
printf ("%s %s\n", c1, c2);
fscanf (fhandle, "%*[^'O']%s%s%s", c3, c4, c5);
printf ("pos %d\n", ftell (fhandle));
printf ("%s %s %s\n", c3, c4, c5);
You will notice that the file position is over 200 at the end of the
program. This leads me to conclude that because of the 'crap' after the
first string this causes problems for the string functions, we therefore
need to ignore this stuff.
This doesn't explain why I need two fscanf()s to do this. Although I have
been testing on a HPUX compiler and this should work.
Regards,
Michael
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
|