-4

I was trying to write a code in c after long time, objective of program is to 1) print the last 10 lines of text file which is received as arguements. 2) display errors otherwise there is a problem with seek command which is got lost correcting it.

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

int main ( int argc, char *argv[] )
{
    char buffer[20],c;
    int bytes =512,flag=0;
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else 
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
             while (1)
             {
                   sprintf (buffer, "seek(file,%d,0)", bytes);
                   system(buffer);
                   while ( (c=fgetc(file))!= EOF)
                   {
                         if(c=='\n')
                         {
                              flag++;
                         }
                   }
                   if (flag >= 10)
                      bytes=bytes*2;
                   else 
                        break;
             }

             flag-=10;
             sprintf (buffer, "seek(file,%d,0)", bytes);
             system(buffer);
             while(flag > 0)
             {
                  if((c=fgetc(file))=='\n')
                  {
                         flag--;
                  }
             }
             while ( (c=fgetc(file))!= EOF)
             {
                         printf("%c",c);
             }
        }
    }
}

Here's the error,

operable program or batch file. 'seek' is not recognized as an internal or external command, operable program or batch file.

John Carter
  • 53,924
  • 26
  • 111
  • 144
Abhilash Muthuraj
  • 2,008
  • 9
  • 34
  • 48

3 Answers3

3

In C, the system() function takes a string and interprets it as a shell command (just as if you typed it on the command line). This is different from a "syscall", which is a low-level call to the OS and is perhaps the source of your confusion here.

It looks like what you want to use is fseek():

fseek(file, bytes, SEEK_SET);

This would replace the calls to sprintf and system.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1
system(buffer);

You are actually doing this : cmd seek. So the error is correct. There is no seek program installed in windows by default.

FailedDev
  • 26,680
  • 9
  • 53
  • 73
0

What is this system command 'seek'? Don't you want C function fseek instead?

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • is fseek same as seek command? I mean syntax is same? – Abhilash Muthuraj Oct 08 '11 at 22:46
  • @Abhilash Muthuraj we don't know because c does not have a `seek` in the language or the standard library. Whatever `seek` you are using comes from the OS, and you haven't said what that is (it's not a POSIX system which also does not have `seek` but does have `lseek`). – dmckee --- ex-moderator kitten Oct 09 '11 at 22:17