> -----Original Message-----
> From: UTTARA@yahoogroups.com [mailto:UTTARA@yahoogroups.com] 
> On Behalf Of Manjunath Naik
> Sent: Sunday, June 26, 2005 2:11 PM
> To: Srikanth Sudhindra; UTTARA@yahoogroups.com
> Subject: Re: [UTTARA] Query
> 
 --- Srikanth Sudhindra 
> wrote:
> 
> > Hi,
> >     I have a query.
> > 
> > 1. There is a file of unknown size having ASCII
> > characters. How do I print
> > the middle character in the file.
> 

> --- Manjunath Naik wrote:
>
> apply the stat system call on the file name
> it has attribute size - that is nothing but the size
> of the file
> 

Using stat(1) for this purpose will be an inefficient
method.  This can be solved without using any commands. 
Following program shows a possibility:


> rest of the thing you know. I hope
> 

No.  I guess he, Sudhindhra, doesn't.  That's why he posed
a question here!

> if not divide the file by two, read that character
> from file. that will be the middle character

This is what I have done.  See the program below. 

> 
> thanks and Regards
> Manjunath
> 

F:\Vijay\C>type print-middle.c

#include 
#include 

int
main (int argc, char *argv[])
{
    FILE *fp;

    if ( argc != 2 )
    {
        fprintf ( stderr, "File name missing.\n" );
        return EXIT_FAILURE;
    }

    fp = fopen ( argv[1], "r" );
    if ( NULL == fp )
    {
        perror (__FILE__);
        return EXIT_FAILURE;
    }

    if ( 0 == fseek (fp, 0, SEEK_END) )
    {
        long bytes;

        bytes = ftell(fp);
        if (-1L != bytes)
        {
            fseek(fp, bytes/2-1, SEEK_SET);
            printf ("The middle character is: %c\n", (char)fgetc(fp));
        }
    }
    else
    {
        perror (__FILE__);
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}
            
F:\Vijay\C>gcc print-middle.c
F:\Vijay\C>type file
1 2 3 4 5 6 7 8 9
F:\Vijay\C>
F:\Vijay\C>a.exe file 
The middle character is: 5



-- 
C is quirky, flawed, and an enormous success.
—Dennis Ritchie





    Source: geocities.com/vijoeyz/faq/c

               ( geocities.com/vijoeyz/faq)                   ( geocities.com/vijoeyz)