/* FGETPOS.C: This program opens a file and reads
* bytes at several different locations.
*/
#include
void main( void )
{
FILE *stream;
fpos_t pos;
char buffer[20];
memset(buffer,0, 20*sizeof(char));
if( (stream = fopen( "fgetpos.c", "rb" )) == NULL )
printf( "Trouble opening file\n" );
else
{
/* Read some data and then check the position. */
fread( buffer, sizeof( char ), 10, stream );
if( fgetpos( stream, &pos ) != 0 )
perror( "fgetpos error" );
else
{
fread( buffer, sizeof( char ), 10, stream );
printf( "10 bytes at byte %I64d %s\n",pos,buffer );
// printf("%s", buffer);
}
/* Set a new position and read more data */
pos = 140;
if( fsetpos( stream, &pos ) != 0 )
perror( "fsetpos error" );
fread( buffer, sizeof( char ), 10, stream );
printf( "10 bytes at byte %I64d: %s\n", pos, buffer );
//printf("%s",buffer);
fclose( stream );
}
}
文章评论(0条评论)
登录后参与讨论