简体   繁体   English

C-一次读取stdin BUFSIZE字符

[英]C - reading from stdin BUFSIZE characters at a time

I'm writing a small sockets program (GNU libc). 我正在编写一个小型套接字程序(GNU libc)。 I have a loop which asks user for input (eg "MSG> "). 我有一个循环,要求用户输入(例如“ MSG>”)。 When the user presses enter the message is sent (currently to a server on localhost). 当用户按下Enter键时,将发送消息(当前发送到localhost上的服务器)。

Anyway, I want to read from stdin into a char buffer[256]. 无论如何,我想从标准输入读入char缓冲区[256]。 I'm currently using fgets() which doesn't do what I want. 我目前正在使用fgets(),它无法满足我的要求。 I'm not sure how write the code such that I ask the user and then get data 256 -1 bytes at a time so that I can send a c-string of 1000 bytes via several strings of 256 bytes. 我不确定如何编写代码以询问用户,然后一次获取256 -1个字节的数据,以便可以通过多个256字节的字符串发送1000字节的c字符串。

EDIT: Add code 编辑:添加代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 256

int main(int argc, char *argv[])
{
  char msg[BUFSIZE];
  size_t msgLen;

  msgLen = strlen(fgets(msg, BUFSIZE, stdin));
  puts(msg);

  // This simply checks whether we managed to fill the buffer and tries to get
  // more input
  while (msgLen == (BUFSIZE - 1))
    {
      memset (msg, '\0', BUFSIZE);
      fread(msg, BUFSIZE, 1, stdin);
      msg[BUFSIZE - 1] = '\0';
      msgLen = strlen(msg);
      puts(msg);
      if (msgLen < (BUFSIZE - 1))
    break;
    }

  return 0;
}

You're implementing a loop to ensure 1000 bytes are recieved, right? 您正在实现一个循环以确保接收到1000个字节,对吗? The loop should indicate that it's counting up to 1000, for the sake of legibility. 为了清晰起见,该循环应表明正在计数到1000。 Keep track of the number of bytes you read (using the += operator), and use that number in the loop condition. 跟踪读取的字节数(使用+ =运算符),并在循环条件中使用该数字。

You seem to assume that fread will read 255 bytes, but this is under the invalid assumption that 255 bytes are available. 您似乎假设fread将读取255个字节,但这是在255个字节可用的无效假设下。 When less than 255 bytes are read, this doesn't necessarily indicate an error; 读取少于255个字节时,这不一定表示错误; keep reading! 继续阅读! When the return value of fread is less than zero, then you should be worried. fread的返回值小于零时,您应该担心。 Make sure you handle those situations. 确保处理这些情况。

You may consider using the read function for buffered input. 您可以考虑将read函数用于缓冲输入。 It takes an open file descriptor (STDIN_FILENO for stdin), a pointer to a buffer ( char * ) and the count of bytes to read. 它需要一个打开的文件描述符(对于stdin为STDIN_FILENO),一个指向缓冲区的指针( char * )和要读取的字节数。 See the manual entry for more details. 有关更多详细信息,请参见手册条目

这个怎么样:

fread(buffer, sizeof(buffer), 1, stdin);

If you're using fgets() , you're using the standard IO library. 如果使用fgets() ,则使用标准的IO库。 You're going to want to use fread() then (instead of, say, read() , which uses a file descriptor), to specify the number of bytes to read. 然后,您将要使用fread() (而不是使用文件描述符的read() )来指定要读取的字节数。 See: http://www.cplusplus.com/reference/cstdio/fread/ 请参阅: http : //www.cplusplus.com/reference/cstdio/fread/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM