我如何在stdin中寻找

时间:2021-05-30 07:28:04

Hello guys I need a help. I want to read from stdin by 16 bytes. Every byte I convert into hexadecimal form. Is there a way I can use read() function to read NOT from the beginning, but for example from the second byte? Also how can I know if I have read the whole stdin? - This way I could call this function in a cycle until I have read the whole stdin

大家好我需要帮助。我想从stdin读取16个字节。我转换成十六进制形式的每个字节。有没有办法我可以使用read()函数从头开始读取NOT,但是例如从第二个字节开始?另外我怎么知道我是否读过整个标准输入? - 这样我就可以在一个循环中调用这个函数,直到我读完整个标准输入

This is a function I made:

这是我做的一个功能:

void getHexLine()
{

  int n = 16;
  char buffer[n];
  read(STDIN_FILENO, buffer, n);
  buffer[n]='\0';
  //printf("%08x", 0); hex number of first byte on line - not working yet
  putchar(' ');
  putchar(' ');
  //converting every byte into hexadecimal
  for (int i = 0;i < 16;i++ )
  {
    printf("%x", buffer[i]);
    putchar(' ');
    if (i == 7 || i == 15)
        putchar(' ');


  }
  printf("|%s|\n", buffer);
} 

The output should be like this but with an option to start from second byte for example.

输出应该是这样的,但可以选择从第二个字节开始。

[vcurda@localhost proj1]$ echo "Hello, world! This is my program." | ./proj1
48 65 6c 6c 6f 2c 20 77  6f 72 6c 64 21 20 54 68  |Hello, world! Th|
69 73 20 69 73 20 6d 79  20 70 72 6f 67 72 61 6d  |is is my program|

This is a school project so I cant use malloc, scanf and <string.h>. I would be really glad if I get some help and sorry for my not very understandable english.

这是一个学校项目,所以我不能使用malloc,scanf和 。如果我得到一些帮助,我会非常高兴,并为我不太懂的英语感到抱歉。

2 个解决方案

#1


2  

Is there a way I can use read() function to read NOT from the beginning, but for example from the second byte?

有没有办法我可以使用read()函数从头开始读取NOT,但是例如从第二个字节开始?

Most universally, you can simply ignore the read-in bytes you're not interested in.

最普遍的是,您可以简单地忽略您不感兴趣的读入字节。

Sometimes you will be able to lseek, e.g. if you run your program with a regular file set to its STDIN as in:

有时你可以lseek,例如如果您运行程序时将常规文件设置为STDIN,如下所示:

./a.out < /etc/passwd

but lseek will fail on STDINs that are terminals, pipes, character devices, or sockets.

但是lseek会在STDIN上失败,这些STDIN是终端,管道,字符设备或套接字。

how can I know if I have read the whole stdin?

我怎么知道我是否读过整个标准输入?

read will return 0 at the end of the file.

read将在文件末尾返回0。


Consult the manual pages for more information. Generally, you should check your return codes and account for short reads. Your function should probably return an int so that it has a way to communicate a possible IO error.

有关更多信息,请参阅手册页。通常,您应该检查您的退货代码和帐户是否有短读。您的函数应该返回一个int,以便它有一种方法来传达可能的IO错误。

#2


1  

stdin is not seekable. You can read bytes in, but you can't rewind or fast forwards. EOF (-1) means end of input in stdin as with a regular file, but it's a bit of a looser concept if you are conducting an interactive dialogue with the user.

stdin不可寻求。您可以读入字节,但无法快退或快进。 EOF(-1)表示stdin中的输入结束与常规文件一样,但如果您正在与用户进行交互式对话,则这是一个更宽松的概念。

Basically stdin is line oriented, and it's best to use the pattern printf() prompt, enter whole line from user, printf() results if applicable and another prompt, read in whole line from user, and so on, at least at first until you get used to programming stdin.

基本上stdin是面向行的,最好使用模式printf()提示,从用户输入整行,printf()结果(如果适用)和另一个提示,从用户读取整行,等等,至少在开始时直到你习惯了编程stdin。

To start from the second byte then becomes easy. Read in the whole line, then start from i = 1 instead of i = 0 as you parse it.

从第二个字节开始然后变得容易。读入整行,然后在解析时从i = 1而不是i = 0开始。

#1


2  

Is there a way I can use read() function to read NOT from the beginning, but for example from the second byte?

有没有办法我可以使用read()函数从头开始读取NOT,但是例如从第二个字节开始?

Most universally, you can simply ignore the read-in bytes you're not interested in.

最普遍的是,您可以简单地忽略您不感兴趣的读入字节。

Sometimes you will be able to lseek, e.g. if you run your program with a regular file set to its STDIN as in:

有时你可以lseek,例如如果您运行程序时将常规文件设置为STDIN,如下所示:

./a.out < /etc/passwd

but lseek will fail on STDINs that are terminals, pipes, character devices, or sockets.

但是lseek会在STDIN上失败,这些STDIN是终端,管道,字符设备或套接字。

how can I know if I have read the whole stdin?

我怎么知道我是否读过整个标准输入?

read will return 0 at the end of the file.

read将在文件末尾返回0。


Consult the manual pages for more information. Generally, you should check your return codes and account for short reads. Your function should probably return an int so that it has a way to communicate a possible IO error.

有关更多信息,请参阅手册页。通常,您应该检查您的退货代码和帐户是否有短读。您的函数应该返回一个int,以便它有一种方法来传达可能的IO错误。

#2


1  

stdin is not seekable. You can read bytes in, but you can't rewind or fast forwards. EOF (-1) means end of input in stdin as with a regular file, but it's a bit of a looser concept if you are conducting an interactive dialogue with the user.

stdin不可寻求。您可以读入字节,但无法快退或快进。 EOF(-1)表示stdin中的输入结束与常规文件一样,但如果您正在与用户进行交互式对话,则这是一个更宽松的概念。

Basically stdin is line oriented, and it's best to use the pattern printf() prompt, enter whole line from user, printf() results if applicable and another prompt, read in whole line from user, and so on, at least at first until you get used to programming stdin.

基本上stdin是面向行的,最好使用模式printf()提示,从用户输入整行,printf()结果(如果适用)和另一个提示,从用户读取整行,等等,至少在开始时直到你习惯了编程stdin。

To start from the second byte then becomes easy. Read in the whole line, then start from i = 1 instead of i = 0 as you parse it.

从第二个字节开始然后变得容易。读入整行,然后在解析时从i = 1而不是i = 0开始。