Re: [Dev-C++] FILE positionning problem.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-07-07 14:59:35
|
First of all - notice that EOF is (-1) while fgetc() returns a unsigned
char cast as an int.
A valid char will then be an integer 0..255, while EOF is -1.
You don't assign to an int but to a char. Then you will not be able to see
the difference between a valid char and the EOF, since (char)-1 is a valid
character.
There is no need to do ftell()/fseek(). fgetc() does work!
int ch;
FILE *Fcarte;
Fcarte =3D foepn(carte,"rt"); /* "rt" for text stream to handle \r\n */
if (Fcarte =3D=3D NULL) {
printf("Failed open file\n");
return;
}
for (;;) {
ch =3D fgetc(Fcarte);
if (ch =3D=3D EOF) break;
printf("I just found the caracter %c (%02x)\n",
/* all characters not printable on all terminals, so just try to
* print 32 .. 127
*/
(ch >=3D ' ' && ch <=3D 127) ? ch : '.',
ch);
}
fclose(Fcarte);
On Mon, 7 Jul 2003, Provic wrote:
> See at the bottom...
>
> Per Westermark wrote:
>
> >Why - aren't we supposted to be clairvoyant and simply "see" the problem
> >before us in the air :-)
> >
> >/Per W
> >
> >On Mon, 7 Jul 2003, Ioannis Vranos wrote:
> >
> >
> >
> >>>-----Original Message-----
> >>>From: dev...@li...
> >>>[mailto:dev...@li...] On Behalf Of Provic
> >>>Sent: Monday, July 07, 2003 11:59 AM
> >>>To: dev...@li...
> >>>Subject: [Dev-C++] FILE positionning problem.
> >>>
> >>>
> >>>Hi !
> >>>My current problem is a little weird : I read in the man-pages that
> >>>fgetc increases automatically the position in a current opened file.
> >>>So my program reads each character of this file in a while :
> >>>it should
> >>>do that, but in fact it doesn't.
> >>>I resolved that by repositionning each time by fseek (param :
> >>>SEEK_SET)
> >>>at the right position ; but a file access monitoring daemon showed me
> >>>that my program isn't optimised at all, it performs 2 fseek which are
> >>>exactly the sames...
> >>>Where is the bug ?
> >>>I verified my code, no need to include it : no pb here.
> >>>
> >>>
> >>I think you had better post your code. :)
> >>
> >>
> >>
> >>
> So that is my code (no comments, they were in french)
>
> void func()
> {
> FILE *Fcarte;
> long int pos; // Position
> char Charou; // The character, converted in char
> int ICharou; // The original character
>
> Fcarte=3Dfopen(carte, "r");
> if (Fcarte=3D=3DNULL)
> {
> printf("Impossible d'ouvrir de fichier bsp.\n");
> return;
> }
>
> while(1)
> {
> pos =3D ftell(Fcarte); //
> _________________Nb 1
> ICharou=3Dfgetc(Fcarte);
> /* if (ICharou=3D=3DEOF) // Here were th=
ere
> an EOF test : I removed it, it did bug a lot (?)
> break; */
>
> Charou=3D(char) ICharou;
> /*
> [stuff, using Charou...] */
>
> fseek(Fcarte, pos+1, SEEK_SET); // _________________Nb=
2
> }
>
> }
>
> before adding Nb1 and Nb2, it worked too buggly (?). thereafter, it
> works too slowly (normal).
>
> Hoping you will help, Thankssssss
>
> Herv=E9
>
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|