NO

Author Topic: _tell and fgetwc problems  (Read 2480 times)

JohnF

  • Guest
_tell and fgetwc problems
« on: June 24, 2014, 02:58:25 PM »
A few problems as I see it.

_tell and _tell64 do not give the current file position.

fgetwc does not in this case increment the file position by 2.

fgetwc returns 0XFFFF on each call, in this case.


Code: [Select]
#include <wchar.h>
#include <stdio.h>
#include <io.h>

int main(void)
{
wchar_t wc;
char c;
long long int pos;
int pos1;
int pos2;
int size;

char buff[700];
FILE * file;
file = fopen("file.c", "rb");
if(file != NULL){
size = _filelength(_fileno(file));
if(size > 700){
fread(buff, 1, 600, file);
c = fgetc(file);
wc = fgetwc(file);
wc = fgetwc(file);
wc = fgetwc(file);
pos = _tell64(_fileno(file));
pos1 = _tell(_fileno(file));
pos2 = ftell(file);
printf("%lld %d %d 0x%x\n", pos, pos1, pos2, wc);
}
fclose(file);
}
return 0;
}

John

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: _tell and fgetwc problems
« Reply #1 on: June 24, 2014, 03:37:52 PM »
Hi John,
First of all tell family refers to lowlevel file system while ftell refers to file buffered streams.
The first reports the OS file pointer position, the other the buffered file position.
Infact your sample reports 1024 for _tell and _tell64, and 601 for ftell. The first is higher because to optimize the IO the buffering is made in chunks of 1kb.
If you try to read more than 1024 bytes tell family output will jump to 2048.
You can crosscheck by removing the stream buffering:
Code: [Select]
char buff[700];
FILE * file;
file = fopen("file.c", "rb");
setvbuf(file, NULL, _IONBF , 0); //Remove stream buffering

The other problem is related to the stream orientation already discussed here.
If you use a wide char as first read the stream orientation changes to wide and both fread and fgetc don't work anymore...  ???
Moreover the tell routines returns position in wide chars and no more in bytes  >:(
Try:
Code: [Select]
if(size > 700){
wc = fgetwc(file);
fread(buff, 1, 600, file);
c = fgetc(file);
wc = fgetwc(file);
wc = fgetwc(file);
:(
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

JohnF

  • Guest
Re: _tell and fgetwc problems
« Reply #2 on: June 24, 2014, 04:28:53 PM »
Hi Frankie,

Ok, I checked with an MS compiler - ftell said 607 and fgetwc read in widechars even after fgetc. I guess we have to accept it.

I take your point about the IO funcs _tell() & _tell64() though.

There should be an ftell64 by now.

EDIT: spelling.

John
« Last Edit: June 24, 2014, 06:08:20 PM by JohnF »