这些系统调用有什么问题?
我在调试这段代码时不知所措。我从指南中复制了示例,该文件是否索引不正确?
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char *inicio(void);
main(void)
{
char *c;
int fd, sz, i;
c = inicio();
fd = open("input.in", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }
sz = read(fd, c, 10);
printf("We have opened input.in, and have called read(%d, c, 10).\n", fd);
printf("read has read %d bytes.\n", sz);
printf("The bytes are these: %s\n", c);
i = lseek(fd, 0, SEEK_CUR);
printf("lseek(%d, 0, SEEK_CUR) returns the current location on the file being %d\n\n", fd, i);
printf("We now look for the start of the file and call read(%d, c, 10)\n",fd);
lseek(fd, 0, SEEK_SET);
sz = read(fd, c, 10);
printf("The reading returns the following bytes: %s\n", c);
printf("We now execute lseek(%d, -6, SEEK_END). and return %d\n",fd, (int) lseek(fd, -6, SEEK_END));
printf("Executing read(%d, c, 10), we get the following bytes: ", fd);
sz = read(fd, c, 10);
printf("Finally, we execute lseek(%d, -1, SEEK_SET). This returns -1.\n", fd);
printf("perror() indicates the fault:\n");
fflush(stdout);
i = lseek(fd, -1, SEEK_SET);
perror("l1");
}
char *inicio(void)
{
char *bytes;
int j;
bytes = (char *) calloc(100, sizeof(char));
for(j=0;j<100;j++){bytes[j]=rand()%32+1;}
return bytes;
}
输入文件是:
Jim Plank
Claxton 221
I'm at a loss debugging this code. I copied the example from a guide, is this file being improperly indexed?
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char *inicio(void);
main(void)
{
char *c;
int fd, sz, i;
c = inicio();
fd = open("input.in", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }
sz = read(fd, c, 10);
printf("We have opened input.in, and have called read(%d, c, 10).\n", fd);
printf("read has read %d bytes.\n", sz);
printf("The bytes are these: %s\n", c);
i = lseek(fd, 0, SEEK_CUR);
printf("lseek(%d, 0, SEEK_CUR) returns the current location on the file being %d\n\n", fd, i);
printf("We now look for the start of the file and call read(%d, c, 10)\n",fd);
lseek(fd, 0, SEEK_SET);
sz = read(fd, c, 10);
printf("The reading returns the following bytes: %s\n", c);
printf("We now execute lseek(%d, -6, SEEK_END). and return %d\n",fd, (int) lseek(fd, -6, SEEK_END));
printf("Executing read(%d, c, 10), we get the following bytes: ", fd);
sz = read(fd, c, 10);
printf("Finally, we execute lseek(%d, -1, SEEK_SET). This returns -1.\n", fd);
printf("perror() indicates the fault:\n");
fflush(stdout);
i = lseek(fd, -1, SEEK_SET);
perror("l1");
}
char *inicio(void)
{
char *bytes;
int j;
bytes = (char *) calloc(100, sizeof(char));
for(j=0;j<100;j++){bytes[j]=rand()%32+1;}
return bytes;
}
the input file is:
Jim Plank
Claxton 221
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您寻求距文件开头的负偏移量,则会收到错误 - 文件开头之前没有可访问的字节。
另外,您应该始终检查或捕获系统调用的结果,而不是依赖于设置的 errno 。 C 库从不将 errno 设置为零(进程/线程启动时除外)。即使函数成功,它也可以设置为非零值。在 Solaris 上,在写入文件后设置 errno 是例行公事,因为该文件不是终端,并且库尝试的操作只能在终端上成功。
工作代码,减去通过“malloc()”分配的内存。它显式地仅打印读取的字符数('
<<%.*s>>
' 将长度限制为给定的大小;尖括号可以轻松查看所读取的数据正在打印)。输出:
If you seek to a negative offset from the start of the file, you get an error - there are no accessible bytes before the beginning of the file.
Also, you should always check or capture the results of system calls, rather than relying on
errno
being set. The C library never setserrno
to zero (other than when the process/thread starts). It can be set to a non-zero value even if the function succeeded. On Solaris, it is routine forerrno
to be set after writing to a file because the file is not a terminal and the library attempts an operation that only succeeds on a terminal.Working code, minus memory allocation via 'malloc()'. It explicitly prints only the number of characters read ('
<<%.*s>>
' limits the length to the size given; the angle brackets make it easy to see the data that is being printed).Output:
printf("字节如下:%s\n", c);
访问冲突(保证一个)。您没有
'\0'
终止符。同样适用于:
printf("读取返回以下字节:%s\n", c);
printf("The bytes are these: %s\n", c);
Access violation (guaranteed one). You don't have a
'\0'
terminator.Same goes for:
printf("The reading returns the following bytes: %s\n", c);
您到底期望文件的第 -1 位置是什么?也许您想要 SEEK_CUR 或 SEEK_END 而不是 SEEK_SET。
What exactly do you expect at the -1th position of the file? Maybe you wanted SEEK_CUR or SEEK_END instead of SEEK_SET.
在 read 调用之后。
after the read calls.