【发布时间】:2011-10-08 22:40:43
【问题描述】:
很久以后我试图用c编写代码,程序的目标是 1) 打印作为参数接收的文本文件的最后 10 行。 2) 否则显示错误 seek 命令有问题,在纠正它时丢失了。
#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);
}
}
}
}
这是错误,
可运行的程序或批处理文件。 'seek' 未被识别为内部或外部命令, 可运行的程序或批处理文件。
【问题讨论】:
-
既然您要向操作系统
seek(file,%d,0)发起攻击,它是否在您的路径中,等等。您可以从命令行运行它吗? -
这是为了练习C吗?如果您确实需要该功能,请使用
tail -n 10。这也是 C++ 还是 C? -
@therefromhere 这是 c 程序,在某些盒式操作系统上的大部分练习。
-
@AbhilashMuthuraj 好的,所以你不需要 C++ 标签,我会删除它。