lseek 问题

发布于 2024-11-09 01:16:21 字数 773 浏览 0 评论 0原文

我很难弄清楚为什么 lseek 无法正常工作。基本上我需要做的就是从标准输入中获取输入,将其存储到名为“log.txt”的文件中,并让程序在遇到“STOP”字样时停止。

我的问题是,当我尝试运行该程序时,出现 lseek 错误:非法查找,但我不知道为什么。我认为问题在于对 lseek 的调用,但如果我用 lseek(fd, atol(0), SEEK_END) 替换 lseek(fd, 0, SEEK_END) ,我会得到一个分段错误。

我现在很迷茫,不知道如何解决这个问题,希望大家给点建议。

int fd;
char buffer[SIZE];
int n, cur;

if (fd = open("log.txt", O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO) < 0)
{
    perror("Error: open");
    return 1;
}

if ((cur = lseek(fd, 0, SEEK_END)) < 0)
{
    printf("Offset corrente: %d\n", (int) lseek(fd, SIZE, SEEK_CUR));
    perror("Error: lseek");
    return 2;
}

while ((n = read(fd, buffer, SIZE)) > 0 && (strncmp(buffer, "STOP", 4) != 0))
{
    write(fd, buffer, SIZE);
}

I'm having a hard time figuring out why lseek doesn't work properly. Basically all I need to do is take input from the standard input, store it into a file named "log.txt" and let the program stop as the "STOP" word is encountered.

My problem is as I try to run the program I get an lseek error: illegal seek and I don't know why. I thought the issue was the call to lseek but if I substitute lseek(fd, 0, SEEK_END) with lseek(fd, atol(0), SEEK_END) I get a segmentation fault.

I'm just so confused, I don't know how to proceed in order to fix this, I hope you could give your advice.

int fd;
char buffer[SIZE];
int n, cur;

if (fd = open("log.txt", O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO) < 0)
{
    perror("Error: open");
    return 1;
}

if ((cur = lseek(fd, 0, SEEK_END)) < 0)
{
    printf("Offset corrente: %d\n", (int) lseek(fd, SIZE, SEEK_CUR));
    perror("Error: lseek");
    return 2;
}

while ((n = read(fd, buffer, SIZE)) > 0 && (strncmp(buffer, "STOP", 4) != 0))
{
    write(fd, buffer, SIZE);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〃安静 2024-11-16 01:16:21
if (fd = open("log.txt", O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO) < 0)

应该是

if ((fd = open("log.txt", O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO)) < 0)

,否则它将被解析为

if (fd = (open("log.txt", O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO) < 0))

(因为 < 的运算符优先级高于 =);如果 open() 成功,它将返回一个 >= 0 的值,因此表达式 open(...) < 0 将为 false,因此 fd 将设置为 0。

文件描述符 0 代表进程的标准输入。除非您已重定向它,否则这将是一个终端设备,对其进行查找是非法的。

if (fd = open("log.txt", O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO) < 0)

should be

if ((fd = open("log.txt", O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO)) < 0)

otherwise it will be parsed as

if (fd = (open("log.txt", O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO) < 0))

(because < has a higher operator precedence than =); if the open() succeeds, it will return a value that is >= 0, so the expression open(...) < 0 will be false, so fd will be set to 0.

File descriptor 0 represents the standard input for your process. Unless you've redirected it, this will be a terminal device, for which seeking is illegal.

讽刺将军 2024-11-16 01:16:21

您正在读取和写入同一个文件

 read(fd, buffer, SIZE)

是错误的。您想从标准输入读取。

You are reading and writing to the same file

 read(fd, buffer, SIZE)

Is wrong. You want to read from stdin.

自找没趣 2024-11-16 01:16:21
if ((cur = lseek(fd, 0, SEEK_END)) < 0)

应更改为:

if ((cur = lseek(fd, 0, SEEK_END)) == -1)

在某些情况下,lseek 可能会提供负偏移量。因此,在测试错误时,仅应使用 x == -1,而不是 x < 0。

if ((cur = lseek(fd, 0, SEEK_END)) < 0)

should be changed to:

if ((cur = lseek(fd, 0, SEEK_END)) == -1)

In certain situations it is possible for lseek to provide a negative offset. As a result when testing for an error, only x == -1 should be used, instead of x < 0.

原来是傀儡 2024-11-16 01:16:21

您不仅读取同一个文件,read() 还读取 SIZE 字节。
除非您的输入是 SIZE 字节的固定长度记录,否则您将读取多行,完全丢失现在位于缓冲区中间的 STOP。

while ( fgets( buffer, sizeof(buffer), stdin) && (strncmp(buffer, "STOP", 4) != 0))

一次读取一行。

Not only are you reading the same file, read() reads SIZE bytes.
Unless your input is fixed length records of SIZE bytes you will read multiple lines, completely missing the STOP which is now in the middle of the buffer.

while ( fgets( buffer, sizeof(buffer), stdin) && (strncmp(buffer, "STOP", 4) != 0))

Reads a line at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文