BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

About fflush() function

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Vladimir Grigoriev
Guest





PostPosted: Fri Feb 10, 2006 5:03 pm    Post subject: About fflush() function Reply with quote



Hi
Look at the code below. It is simple C-program test (BCB 5 console
application).

#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop

#include <conio.h>
//--------------------------------------------------------------------------
-

#pragma argsused
int main(int argc, char* argv[])
{
FILE *fp, *fp2;
int rc, rc2, rc3, rc4;

fp = fopen( "c:\\file.txt", "w+" );
fprintf( fp, "first record" );

fp2 = fopen( "c:\\file.txt", "r" );
rc = fgetc( fp2 );
printf( "return code is %i\n", rc );

fputc( '\n', fp );
fflush( fp );

rc2 = fgetc( fp2 );
printf( "value is now %c\n", rc2 );

rewind( fp );

fprintf( fp, "some updates\n" );
rc3 = fgetc( fp2 );
printf( "value is now %c\n", rc3 );

fflush( fp );
fflush( fp2 );

rc4 = fgetc( fp2 );
printf( "value is now %c\n", rc4 );

printf( "\nPress any key to exit..." );
getch();
return EXIT_SUCCESS;
}

The program output is

return code is -1
value is now f
value is now i
value is now r

Press any key to exit...

My question is why is the last "value is now" "r"? I thought that after
fflush( fp );
fflush( fp2 );

the last "value is now" must be "m" as the new string "some updates\n" was
written.
What is wrong?

Vladimir Grigoriev
Back to top
Alan Bellingham
Guest





PostPosted: Fri Feb 10, 2006 5:03 pm    Post subject: Re: About fflush() function Reply with quote



"Vladimir Grigoriev" <vlad.moscow (AT) mail (DOT) ru> wrote:

Quote:
Hi
Look at the code below. It is simple C-program test (BCB 5 console
application).

#include <stdio.h
#include <stdlib.h
#pragma hdrstop

#include <conio.h
//--------------------------------------------------------------------------
-

#pragma argsused
int main(int argc, char* argv[])
{
FILE *fp, *fp2;
int rc, rc2, rc3, rc4;

fp = fopen( "c:\\file.txt", "w+" );
fprintf( fp, "first record" );

fp2 = fopen( "c:\\file.txt", "r" );

An odd way of doing things! I assume this is a test so that you can see
whether output flushing works, though.

Quote:
rc = fgetc( fp2 );
printf( "return code is %i\n", rc );

fputc( '\n', fp );
fflush( fp );

Appends the newline.

Quote:
rc2 = fgetc( fp2 );
printf( "value is now %c\n", rc2 );

Gets first character of input file. This is the 'f' of 'first record'.

Quote:
rewind( fp );

fprintf( fp, "some updates\n" );
rc3 = fgetc( fp2 );

Gets second character of input file. This is the 'i' of 'first record'.

Quote:
printf( "value is now %c\n", rc3 );

fflush( fp );

Flushes the output - so that if 'some updates' isn't already there on
disc, it is now.

Quote:
fflush( fp2 );

A rather pointless call that does nothing. (fflush() flushes data out to
a write file. It is documented not to have any defined behaviour for
input files.)

Quote:
rc4 = fgetc( fp2 );

Gets third character of input file. This is the 'r' of 'first record' -
at least, it was 'r' when the buffer was read, and even though the
buffer doesn't represent the current disc contents, that's what it read
and it hasn't seen any reason to update.

Quote:
My question is why is the last "value is now" "r"? I thought that after
fflush( fp );
fflush( fp2 );

the last "value is now" must be "m" as the new string "some updates\n" was
written.
What is wrong?

You have picked up the incorrect idea that fflush() works on an input
stream. It does not. You have also come up with the more unusual idea
that when working on an input file, fflush() empties the current buffer
and causes it to be reread. (This is an interesting idea, and almost
worth submitting to the standardisation committees. It's certainly more
interesting than the common idea that fflush(stdin) will empty the input
stream.)

What you need to do to achieve what you appear to want to do is to turn
off read buffering. Use setbuf() or setvbuf() on the read file. Check
the docs for details.

Note that unbuffered reading may well be much, much slower than buffered
reading.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
Back to top
Ed Mulroy
Guest





PostPosted: Fri Feb 10, 2006 5:03 pm    Post subject: Re: About fflush() function Reply with quote



Before you go any further add code to check that the value of the pointer
returned by each of the fopen calls is not NULL and report the error to the
screen if it is.

.. Ed

Quote:
Vladimir Grigoriev wrote in message
news:43ecb9cf (AT) newsgroups (DOT) borland.com...
Hi
Look at the code below. It is simple C-program test (BCB 5 console
application).

#include <stdio.h
#include <stdlib.h
#pragma hdrstop

#include <conio.h
//--------------------------------------------------------------------------
-

#pragma argsused
int main(int argc, char* argv[])
{
FILE *fp, *fp2;
int rc, rc2, rc3, rc4;

fp = fopen( "c:\\file.txt", "w+" );
fprintf( fp, "first record" );

fp2 = fopen( "c:\\file.txt", "r" );
rc = fgetc( fp2 );
printf( "return code is %i\n", rc );

fputc( '\n', fp );
fflush( fp );

rc2 = fgetc( fp2 );
printf( "value is now %c\n", rc2 );

rewind( fp );

fprintf( fp, "some updates\n" );
rc3 = fgetc( fp2 );
printf( "value is now %c\n", rc3 );

fflush( fp );
fflush( fp2 );

rc4 = fgetc( fp2 );
printf( "value is now %c\n", rc4 );

printf( "\nPress any key to exit..." );
getch();
return EXIT_SUCCESS;
}

The program output is

return code is -1
value is now f
value is now i
value is now r

Press any key to exit...

My question is why is the last "value is now" "r"? I thought that after
fflush( fp );
fflush( fp2 );

the last "value is now" must be "m" as the new string "some updates\n" was
written.
What is wrong?
Back to top
Vladimir Grigoriev
Guest





PostPosted: Mon Feb 13, 2006 10:03 am    Post subject: Re: About fflush() function Reply with quote

Hi Ed
There is not a big essence to include error checking in this simple test
program. I tried to make the program as simple as possible.
For me the question about how flushing buffer influences on read/write
operations is more important.

Vladimir Grigoriev

"Ed Mulroy" <dont_email_me (AT) bitbuc (DOT) ket> wrote in message
news:43ecc070$1 (AT) newsgroups (DOT) borland.com...
Quote:
Before you go any further add code to check that the value of the pointer
returned by each of the fopen calls is not NULL and report the error to
the
screen if it is.

. Ed

Vladimir Grigoriev wrote in message
news:43ecb9cf (AT) newsgroups (DOT) borland.com...
Hi
Look at the code below. It is simple C-program test (BCB 5 console
application).

#include <stdio.h
#include <stdlib.h
#pragma hdrstop

#include <conio.h

//--------------------------------------------------------------------------
-

#pragma argsused
int main(int argc, char* argv[])
{
FILE *fp, *fp2;
int rc, rc2, rc3, rc4;

fp = fopen( "c:\\file.txt", "w+" );
fprintf( fp, "first record" );

fp2 = fopen( "c:\\file.txt", "r" );
rc = fgetc( fp2 );
printf( "return code is %i\n", rc );

fputc( '\n', fp );
fflush( fp );

rc2 = fgetc( fp2 );
printf( "value is now %c\n", rc2 );

rewind( fp );

fprintf( fp, "some updates\n" );
rc3 = fgetc( fp2 );
printf( "value is now %c\n", rc3 );

fflush( fp );
fflush( fp2 );

rc4 = fgetc( fp2 );
printf( "value is now %c\n", rc4 );

printf( "\nPress any key to exit..." );
getch();
return EXIT_SUCCESS;
}

The program output is

return code is -1
value is now f
value is now i
value is now r

Press any key to exit...

My question is why is the last "value is now" "r"? I thought that after
fflush( fp );
fflush( fp2 );

the last "value is now" must be "m" as the new string "some updates\n"
was
written.
What is wrong?

Back to top
Alan Bellingham
Guest





PostPosted: Mon Feb 13, 2006 11:03 am    Post subject: Re: About fflush() function Reply with quote

"Vladimir Grigoriev" <vlad.moscow (AT) mail (DOT) ru> wrote:

Quote:
Hi Alan
I have taken this example from IBM z/OS C/C++ Programming Guide. Here is a
text from the guide

Then their example is an extension to Standard C++, probably specific to
that operating system only.

Standard C++ doesn't work that way. On the other hand, if you want to be
reading and writing to the same file from the same program, then the
solution is not to open it twice, but only once. I was assuming that you
were showing the read and write parts from two different programs, but
in a single program to make it synchronous.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
Back to top
Vladimir Grigoriev
Guest





PostPosted: Mon Feb 13, 2006 11:03 am    Post subject: Re: About fflush() function Reply with quote

Also I have looked through help documentation of BCB 5 and I have found
function _flushall(). But this function flushes all buffers of input and
output files. It has not a parameter as FILE * to flush a buffer of a
specified file.

Vladimir Grigoriev

"Alan Bellingham" <alanb (AT) episys (DOT) com> wrote in message
news:4ffpu156q6dk9on344du59lsu4rqg2fg20 (AT) 4ax (DOT) com...
Quote:
"Vladimir Grigoriev" <vlad.moscow (AT) mail (DOT) ru> wrote:

Hi
Look at the code below. It is simple C-program test (BCB 5 console
application).

#include <stdio.h
#include <stdlib.h
#pragma hdrstop

#include <conio.h

//-------------------------------------------------------------------------
-
-

#pragma argsused
int main(int argc, char* argv[])
{
FILE *fp, *fp2;
int rc, rc2, rc3, rc4;

fp = fopen( "c:\\file.txt", "w+" );
fprintf( fp, "first record" );

fp2 = fopen( "c:\\file.txt", "r" );

An odd way of doing things! I assume this is a test so that you can see
whether output flushing works, though.

rc = fgetc( fp2 );
printf( "return code is %i\n", rc );

fputc( '\n', fp );
fflush( fp );

Appends the newline.

rc2 = fgetc( fp2 );
printf( "value is now %c\n", rc2 );

Gets first character of input file. This is the 'f' of 'first record'.

rewind( fp );

fprintf( fp, "some updates\n" );
rc3 = fgetc( fp2 );

Gets second character of input file. This is the 'i' of 'first record'.

printf( "value is now %c\n", rc3 );

fflush( fp );

Flushes the output - so that if 'some updates' isn't already there on
disc, it is now.

fflush( fp2 );

A rather pointless call that does nothing. (fflush() flushes data out to
a write file. It is documented not to have any defined behaviour for
input files.)

rc4 = fgetc( fp2 );

Gets third character of input file. This is the 'r' of 'first record' -
at least, it was 'r' when the buffer was read, and even though the
buffer doesn't represent the current disc contents, that's what it read
and it hasn't seen any reason to update.

My question is why is the last "value is now" "r"? I thought that after
fflush( fp );
fflush( fp2 );

the last "value is now" must be "m" as the new string "some updates\n"
was
written.
What is wrong?

You have picked up the incorrect idea that fflush() works on an input
stream. It does not. You have also come up with the more unusual idea
that when working on an input file, fflush() empties the current buffer
and causes it to be reread. (This is an interesting idea, and almost
worth submitting to the standardisation committees. It's certainly more
interesting than the common idea that fflush(stdin) will empty the input
stream.)

What you need to do to achieve what you appear to want to do is to turn
off read buffering. Use setbuf() or setvbuf() on the read file. Check
the docs for details.

Note that unbuffered reading may well be much, much slower than buffered
reading.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
Back to top
Vladimir Grigoriev
Guest





PostPosted: Mon Feb 13, 2006 11:03 am    Post subject: Re: About fflush() function Reply with quote

Hi Alan
I have taken this example from IBM z/OS C/C++ Programming Guide. Here is a
text from the guide

Reading Updated Records

If you have a file open for read at the same time that the file is open for
write in thesame application, you will be able to see the new data if you
call fflush() torefresh the contents of the input buffer, as in the
following example:

/* this example demonstrates how updated records are read */

#include <stdio.h>

int main(void)

{

FILE * fp, * fp2;

int rc, rc2, rc3, rc4;

fp = fopen("a.b","w+");

fprintf(fp,"first record");

fp2 = fopen("a.b","r"); /* Simultaneous Reader */

/* following gets EOF since fp has not completed first line

* of output so nothing will be flushed to file yet */

rc = fgetc(fp2);

printf("return code is %i\n", rc);

fputc('\n', fp); /* this will complete first line */

fflush(fp); /* ensures data is flushed to file */

rc2 = fgetc(fp2); /* this gets 'f' from first record */

printf("value is now %c\n", rc2);

rewind(fp);

fprintf(fp, "some updates\n");

rc3 = fgetc(fp2); /* gets 'i' ..doesn't know about update */

printf("value is now %c\n", rc3);

fflush(fp); /* ensure update makes it to file */

fflush(fp2); /* this updates reader's buffer */

rc4 = fgetc(fp2); /* gets 'm', 3rd char of updated record */

printf("value is now %c\n", rc4);

return(0);

}

So I tried to repeat the test for BCB 5 (console mode) but as you can see
the result is different...

Vladimir Grigoriev

"Alan Bellingham" <alanb (AT) episys (DOT) com> wrote in message
news:4ffpu156q6dk9on344du59lsu4rqg2fg20 (AT) 4ax (DOT) com...

Quote:
"Vladimir Grigoriev" <vlad.moscow (AT) mail (DOT) ru> wrote:

Hi
Look at the code below. It is simple C-program test (BCB 5 console
application).

#include <stdio.h
#include <stdlib.h
#pragma hdrstop

#include <conio.h

//-------------------------------------------------------------------------
-
-

#pragma argsused
int main(int argc, char* argv[])
{
FILE *fp, *fp2;
int rc, rc2, rc3, rc4;

fp = fopen( "c:\\file.txt", "w+" );
fprintf( fp, "first record" );

fp2 = fopen( "c:\\file.txt", "r" );

An odd way of doing things! I assume this is a test so that you can see
whether output flushing works, though.

rc = fgetc( fp2 );
printf( "return code is %i\n", rc );

fputc( '\n', fp );
fflush( fp );

Appends the newline.

rc2 = fgetc( fp2 );
printf( "value is now %c\n", rc2 );

Gets first character of input file. This is the 'f' of 'first record'.

rewind( fp );

fprintf( fp, "some updates\n" );
rc3 = fgetc( fp2 );

Gets second character of input file. This is the 'i' of 'first record'.

printf( "value is now %c\n", rc3 );

fflush( fp );

Flushes the output - so that if 'some updates' isn't already there on
disc, it is now.

fflush( fp2 );

A rather pointless call that does nothing. (fflush() flushes data out to
a write file. It is documented not to have any defined behaviour for
input files.)

rc4 = fgetc( fp2 );

Gets third character of input file. This is the 'r' of 'first record' -
at least, it was 'r' when the buffer was read, and even though the
buffer doesn't represent the current disc contents, that's what it read
and it hasn't seen any reason to update.

My question is why is the last "value is now" "r"? I thought that after
fflush( fp );
fflush( fp2 );

the last "value is now" must be "m" as the new string "some updates\n"
was
written.
What is wrong?

You have picked up the incorrect idea that fflush() works on an input
stream. It does not. You have also come up with the more unusual idea
that when working on an input file, fflush() empties the current buffer
and causes it to be reread. (This is an interesting idea, and almost
worth submitting to the standardisation committees. It's certainly more
interesting than the common idea that fflush(stdin) will empty the input
stream.)

What you need to do to achieve what you appear to want to do is to turn
off read buffering. Use setbuf() or setvbuf() on the read file. Check
the docs for details.

Note that unbuffered reading may well be much, much slower than buffered
reading.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Mon Feb 13, 2006 5:03 pm    Post subject: Re: About fflush() function Reply with quote

Please direct your browser at http://info.borland.com/newsgroups/ and
read the newsgroup guidelines. One of them asks us not to quote entire
posts we are following up to; instead, please trim the quotes to the
parts relevant for your reply. Thanks!
Back to top
Vladimir Grigoriev
Guest





PostPosted: Tue Feb 14, 2006 10:03 am    Post subject: Re: About fflush() function Reply with quote

O'k
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.