On Wed, 4 Sep 2002 11:37:42 -0700, "gordon smith"
Quote:
>I have a question about fscanf using the MS C++ compiler
>version 12.00.8804 for 80x86 and the corresponding library.
>We have source that we trust functions properly in Unix,
>but we are now compiling a console application in Visual
>C++ and have a one byte file position error involving
>fscanf.
>We have a file as follows:
>File
>pos. Char.
>---- --------
>286: 0x36 = '6'
>287: 0x35 = '5'
>288: 0x35 = '5'
>289: 0x33 = '3'
>290: 0x36 = '6'
>291: 0x20 = ' '
>292: 0x33 = '3'
>293: 0x36 = '6'
>294: 0x0a = <LF>
>295: 0x00 (data begins here)
>296: 0x00
>297: 0xab
>298: 0xfd
>At the start, the position in the file is at 286.
>The original source uses this statement:
> fscanf( fpData, "%d %d\n", &num0, &num1 );
>which leaves the file position at 296. This appears to be
>incorrect. It correctly returns 2 as the number of items
>read.
Surely you meant 294. Reading another character as you describe below
should not move you backwards in the file.
Quote:
>We can use the following statement:
> fscanf( fpData, "%d %d%c", &num0, &num1, &ch );
>which leaves the position at 295, as we expect.
>Is the behaviour of the first statement a defect? If so,
>is there a source of information or a fix available?
I don't think so.
The first %d stops at the first character that cannot be part of
the integer. This would be the blank at 291.
The second %d skips over white space, process the digits, and
stops at the next character that cannot be part of the integer. This
is the LF at 294.
If you add the %c, it will accept the LF and leave you at 295.
If the file positions are fixed, you might be better off with fread
and sscanf.
You don't describe how it works in Unix but it should be the same.
Quote:
<<Remove the del for email>>