Re: Why can't lseek the STDIN_FILENO?

Поиск
Список
Период
Сортировка
От John McKown
Тема Re: Why can't lseek the STDIN_FILENO?
Дата
Msg-id CAAJSdjiETS2skporc3xWMU2geHgTiu5PSx3K6ovgJhJ+gpwYrw@mail.gmail.com
обсуждение исходный текст
Ответ на Why can't lseek the STDIN_FILENO?  ("Wen Yi" <896634148@qq.com>)
Ответы Re: Why can't lseek the STDIN_FILENO?
Список pgsql-general
My best advice would be to ask a C language question on a C language forum. This forum is really only for questions about the SQL language for the PostgreSQL database. I.e. no MariaDB, MySQL, MS SQL questions.

First, you didn't say what OS and she'll you're using. I an guessing BASH and Linux. 

Second, you did NO error checking. I would purely guess that the lseek() is getting a return value of -1, probably with an error of ESPIPE. 


The bottom line from the above post is that STDIN is not seekable when it is a terminal.

On Fri, Jun 23, 2023, 21:17 Wen Yi <896634148@qq.com> wrote:
Hi community,
I am testing the lseek & write & read, and I write the code like this:

/*
    lseek_test.c
        Test the lseek
    Wen Yi
*/
#include <unistd.h>
#include <fcntl.h>
int main()
{
    int fd = 0;
    char buffer[16] = {};
    write(STDIN_FILENO, "Hello world\n", sizeof("Hello world\n"));
    lseek(STDIN_FILENO, 0, SEEK_SET);
    read(STDIN_FILENO, buffer, sizeof(buffer));
    write(STDIN_FILENO, buffer, sizeof(buffer));
    return 0;
}

And I run the program ("Something Input" is my input content)

[beginnerc@bogon 学习 C语言]$ gcc lseek_test.c
[beginnerc@bogon 学习 C语言]$ ./a.out
Hello world
Something Input
Something Input
[beginnerc@bogon 学习 C语言]$

I really don't know, why the buffer's content not be "Hello world\n"? (I use the lseek to move the cursor to the beginning region)

Can someone give me some advice?
Thanks in advance!

Yours,
Wen Yi

В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Why can't lseek the STDIN_FILENO?
Следующее
От: Avin Kavish
Дата:
Сообщение: Accessing referential constraint information with minimal permissions