在c中使用lseek命令获取文件大小
我被要求查找文件usjnglseek命令的大小(不使用stat),我编写了以下代码在c中使用lseek命令获取文件大小,c,operating-system,C,Operating System,我被要求查找文件usjnglseek命令的大小(不使用stat),我编写了以下代码 但是我的文件大小是-1,我在哪里出了问题 返回值 成功完成后,lseek()返回结果偏移量 从文件开头开始的位置(以字节为单位)打开 错误,返回值(off_t)-1,并将errno设置为指示 错误。 因此,您必须检查errno(如果lseek返回-1,则打印它): 来自同一事件的可能错误列表: 错误 EBADF fd is not an open file descriptor. EINVA
但是我的文件大小是-1,我在哪里出了问题 返回值
成功完成后,lseek()返回结果偏移量 从文件开头开始的位置(以字节为单位)打开 错误,返回值(off_t)-1,并将errno设置为指示 错误。
因此,您必须检查
errno
(如果lseek
返回-1
,则打印它):
来自同一事件的可能错误列表:
错误
EBADF fd is not an open file descriptor.
EINVAL whence is not valid. Or: the resulting file offset would be
negative, or beyond the end of a seekable device.
ENXIO whence is SEEK_DATA or SEEK_HOLE, and the file offset is
beyond the end of the file.
EOVERFLOW The resulting file offset cannot be represented in an off_t.
ESPIPE fd is associated with a pipe, socket, or FIFO.
在您的情况下,最有可能是EBADF。来自
lseek
文档:
返回值成功完成后,lseek()返回结果偏移量 从文件开头开始的位置(以字节为单位)打开 错误,返回值(off_t)-1,并将errno设置为指示 错误。
因此,您必须检查
errno
(如果lseek
返回-1
,则打印它):
来自同一事件的可能错误列表:
错误
EBADF fd is not an open file descriptor.
EINVAL whence is not valid. Or: the resulting file offset would be
negative, or beyond the end of a seekable device.
ENXIO whence is SEEK_DATA or SEEK_HOLE, and the file offset is
beyond the end of the file.
EOVERFLOW The resulting file offset cannot be represented in an off_t.
ESPIPE fd is associated with a pipe, socket, or FIFO.
在您的情况下,最有可能是EBADF。以下建议的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main( void )
{
char fn[10];
int fd;
printf("Enter file name\n");
if( scanf("%9s", fn) != 1 )
{
fprintf( stderr, "scanf for file name failed\n" );
exit( EXIT_FAILURE );
}
if( (fd = open(fn, O_RDONLY) ) < 0 )
{
perror( "open failed" );
exit( EXIT_FAILURE );
}
off_t size = lseek(fd, 0, SEEK_END);
printf("Size is %ld", size);
close(fd);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main( void )
{
char fn[10];
int fd;
printf("Enter file name\n");
if( scanf("%9s", fn) != 1 )
{
fprintf( stderr, "scanf for file name failed\n" );
exit( EXIT_FAILURE );
}
if( (fd = open(fn, O_RDONLY) ) < 0 )
{
perror( "open failed" );
exit( EXIT_FAILURE );
}
off_t size = lseek(fd, 0, SEEK_END);
printf("Size is %ld", size);
close(fd);
}
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
char-fn[10];
int-fd;
printf(“输入文件名\n”);
如果(扫描频率(“%9s”,fn)!=1)
{
fprintf(stderr,“扫描文件名失败\n”);
退出(退出失败);
}
如果((fd=打开(仅fn,Ordu))<0)
{
perror(“开放式失败”);
退出(退出失败);
}
off_t size=lseek(fd,0,SEEK_END);
printf(“大小为%ld”,大小);
关闭(fd);
}
以下建议的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main( void )
{
char fn[10];
int fd;
printf("Enter file name\n");
if( scanf("%9s", fn) != 1 )
{
fprintf( stderr, "scanf for file name failed\n" );
exit( EXIT_FAILURE );
}
if( (fd = open(fn, O_RDONLY) ) < 0 )
{
perror( "open failed" );
exit( EXIT_FAILURE );
}
off_t size = lseek(fd, 0, SEEK_END);
printf("Size is %ld", size);
close(fd);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main( void )
{
char fn[10];
int fd;
printf("Enter file name\n");
if( scanf("%9s", fn) != 1 )
{
fprintf( stderr, "scanf for file name failed\n" );
exit( EXIT_FAILURE );
}
if( (fd = open(fn, O_RDONLY) ) < 0 )
{
perror( "open failed" );
exit( EXIT_FAILURE );
}
off_t size = lseek(fd, 0, SEEK_END);
printf("Size is %ld", size);
close(fd);
}
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
char-fn[10];
int-fd;
printf(“输入文件名\n”);
如果(扫描频率(“%9s”,fn)!=1)
{
fprintf(stderr,“扫描文件名失败\n”);
退出(退出失败);
}
如果((fd=打开(仅fn,Ordu))<0)
{
perror(“开放式失败”);
退出(退出失败);
}
off_t size=lseek(fd,0,SEEK_END);
printf(“大小为%ld”,大小);
关闭(fd);
}
手册上说lseek返回的是什么?对我来说很好。您确定要输入的文件名在正确的目录中吗?lseek()将与文件描述符fd关联的打开文件描述的文件偏移量重新定位为参数偏移量,因为您没有检查open()
是否成功,最可能的问题是您没有成功打开文件,因此lseek()
失败并报告-1
。您只允许使用9个字符的文件名(加上终端空字节);时间不长!如果在lseek()
之后选中errno
(来自
),您可能会发现它包含EBADF
(在close()
之后也是如此)。检查scanf()。您确定要输入的文件名在正确的目录中吗?lseek()将与文件描述符fd关联的打开文件描述的文件偏移量重新定位为参数偏移量,因为您没有检查open()
是否成功,最可能的问题是您没有成功打开文件,因此lseek()
失败并报告-1
。您只允许使用9个字符的文件名(加上终端空字节);时间不长!如果在lseek()
之后选中errno
(来自
),您可能会发现它包含EBADF
(在close()
之后也是如此)。检查scanf()。