Thread: [Dev-C++] FILE positionning problem.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Provic <pr...@wa...> - 2003-07-07 08:57:49
|
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 would be pleased if you could help me ! Thanks a lot ! Hervé |
|
From: Ioannis V. <iv...@em...> - 2003-07-07 10:47:25
|
> -----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. :) Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Per W. <pw...@ia...> - 2003-07-07 13:02:24
|
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. :) > > > > > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL 1: http://run.to/noicys > * Alternative URL 2: http://www.noicys.cjb.net > > > > > ------------------------------------------------------- > 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 > |
|
From: Provic <pr...@wa...> - 2003-07-07 14:29:25
|
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=fopen(carte, "r");
if (Fcarte==NULL)
{
printf("Impossible d'ouvrir de fichier bsp.\n");
return;
}
while(1)
{
pos = ftell(Fcarte); //
_________________Nb 1
ICharou=fgetc(Fcarte);
/* if (ICharou==EOF) // Here were there
an EOF test : I removed it, it did bug a lot (?)
break; */
Charou=(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é
|
|
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
>
|
|
From: Ioannis V. <iv...@em...> - 2003-07-07 15:56:54
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Provic
> Sent: Monday, July 07, 2003 5:31 PM
> To: dev...@li...
> Subject: Re: [Dev-C++] FILE positionning problem.
>
> void func()
I assume you play in C, so make th above void func(void)
void func() and void func(void) are not the same thing in C. In C empty
parenthesis means to not make any argument checking.
For example if you do func(7); and func(NULL); you will get no error
messages.
> {
> FILE *Fcarte;
> long int pos; // Position
> char Charou; // The character, converted in char
> int ICharou; // The original character
>
> Fcarte=fopen(carte, "r");
What is carte. It should be "carte".
> if (Fcarte==NULL)
> {
> printf("Impossible d'ouvrir de fichier bsp.\n");
> return;
> }
>
> while(1)
> {
> pos = ftell(Fcarte);
Useless.
//
> _________________Nb 1
> ICharou=fgetc(Fcarte);
> /* if (ICharou==EOF) // Here
> were there
> an EOF test : I removed it, it did bug a lot (?)
> break; */
>
> Charou=(char) ICharou;
There is no need for casting here.
> /*
> [stuff, using Charou...] */
>
> fseek(Fcarte, pos+1, SEEK_SET);
Useless (in theory, but you said you had problems?).
> _________________Nb 2
> }
>
> }
>
> before adding Nb1 and Nb2, it worked too buggly (?). thereafter, it
> works too slowly (normal).
>
> Hoping you will help, Thankssssss
Assuming you have the file named "carte" (without any extension) run the
following code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *Fcarte=fopen("carte" ,"r");
char c;
int input;
if(Fcarte==NULL)
{
fprintf(stderr, "\nMen the lifeboats. Women and children
first!\n\n");
return EXIT_FAILURE;
}
while((input=fgetc(Fcarte))!=EOF)
{
c=input;
/* Do whatever you want with c */
printf("File pos: %ld\n", ftell(Fcarte));
}
printf("\n\nIn theory the file pointer was advancing 1 long at a time\n");
return 0;
}
The results in my system:
C:\c>copy con carte
I have a mission to my life
^Z
1 file(s) copied.
C:\c>temp
File pos: 1
File pos: 2
File pos: 3
File pos: 4
File pos: 5
File pos: 6
File pos: 7
File pos: 8
File pos: 9
File pos: 10
File pos: 11
File pos: 12
File pos: 13
File pos: 14
File pos: 15
File pos: 16
File pos: 17
File pos: 18
File pos: 19
File pos: 20
File pos: 21
File pos: 22
File pos: 23
File pos: 24
File pos: 25
File pos: 26
File pos: 27
File pos: 29
In theory the file pointer was advancing 1 long at a time
C:\c>
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|
|
From: Provic <pr...@wa...> - 2003-07-07 14:35:21
|
>
>
>> 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=fopen(carte, "r");
if (Fcarte==NULL)
{
printf("Impossible d'ouvrir de fichier bsp.\n");
return;
}
while(1)
{
pos = ftell(Fcarte); //
_________________Nb 1
ICharou=fgetc(Fcarte);
/* if (ICharou==EOF) // Here were there
an EOF test : I removed it, it did bug a lot (?)
break; */
Charou=(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é
|
|
From: <a_g...@ya...> - 2003-07-07 17:20:56
Attachments:
testFile.c.htm
|
<html>
<head>
<title>testFile.c</title>
</head>
<!-- Generated by SynEdit HTML exporter -->
<body text="#000000" bgcolor="#FFFFFF">
<pre>
<code><font size=3 face="Courier New"><font color="#808000">#include <stdio.h>
</font><font color="#0000FF">int </font>main(<font color="#0000FF">int </font>argc,<font color="#0000FF">char </font>**argv)
{
FILE *fp=NULL;
<font color="#0000FF">unsigned long </font>pos=<font color="#FF00FF">0</font>;
<font color="#0000FF">char </font>c=<font color="#993366">'\0'</font>;
<font color="#0000FF">if</font>(argc == <font color="#FF00FF">1</font>){
fprintf(stderr,<font color="#993366">"Usage: %s <filename>"</font>,argv[<font color="#FF00FF">0</font>]);
exit(<font color="#FF00FF">1</font>);
}<font color="#008000">/*if no command line arguments*/
</font><font color="#0000FF">if</font>((fp=fopen(argv[<font color="#FF00FF">1</font>],<font color="#993366">"r"</font>))==NULL){
fprintf(stderr,<font color="#993366">"Could not open <%s>\n"</font>,argv[<font color="#FF00FF">1</font>]);
exit(<font color="#FF00FF">1</font>);
}<font color="#008000">/*if file open fails*/
</font><font color="#0000FF">while</font>(!feof(fp)){
c=fgetc(fp);
putchar(c);
pos=ftell(fp);
printf(<font color="#993366">"\nPosition now %d\n"</font>,pos);
}<font color="#008000">/*while file does not end*/
</font>fclose(fp);
printf(<font color="#993366">"\n\n Bye bye \n\n"</font>);
<font color="#0000FF">return </font>getchar();
}<font color="#008000">/*main*/
</font></font>
</code></pre>
</body>
</html> |