首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C getline()实现

C getline()实现
EN

Code Review用户
提问于 2016-02-07 20:34:48
回答 2查看 7.9K关注 0票数 7

我正在练习我的C编码,为了学习目的,我想在C中实现我自己版本的getline函数。我想回顾一下代码的风格、正确性、对性能的改进以及代码的总体质量。我主要关心的是正确性和性能(按顺序排列)。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_LINE_LENGTH 255 

/* Retrieves a line of text from the stream provided
 * and places it into @buf until a new line character is
 * reached or the number of characters read is > @size - 1.
 * This function will null-terminate the provided buffer.
 *
 * @param[in] -- stream -- the stream
 * @param[in] -- buf -- a buffer big enough for @size chars.
 * @param[in] -- size -- the maximum number of chars to read (must
 *    include room for a null terminator
 * @return -- the number of characters read from the stream.
 */
size_t getline(FILE *stream, char *buf, size_t size)
{
    size_t count = 0;
    char c;
    while ((c = (char)getc(stream)) != '\n' && count < size - 1) {
        buf[count++] = c;
    }
    buf[count] = '\0';
    return count;
}

int main()
{
   char line[MAX_LINE_LENGTH];
   while (true) {
       size_t count = getline(stdin, line, MAX_LINE_LENGTH);
       printf("The line gotten was \"%s\" and was %zu chars long.\n", line, count);
   }
}
EN

回答 2

Code Review用户

发布于 2016-02-07 22:27:51

  • 您的getline函数看起来更像是fgets的变体,而不是getline
  • 如果size == 0size - 1 == SIZE_MAX,一个很大的数字。
  • 您的getline从流读取到size字节,尽管它只将size - 1放置到缓冲区中。它只是悄悄地删除了最后一个字节。您应该切换循环条件的顺序: while (count < size & (c = (char)getc(stream)) != '\n') {.}
  • getline写入终止空字节时,如果size为0时,它也会将其参数定义的范围外的内存写入内存中。
  • 如果getc(stream) == EOF是一个错误条件,您的getline函数将继续尝试从流中读取,始终“读取”EOF,将(char) EOF (通常是'\xff')放置到缓冲区中,直到到达缓冲区的末尾。您可以通过将stdin指向一个空文件来尝试此操作。
  • 如果使用迭代计数器,则使用for循环而不是while循环。它会更清楚,什么是增加,地点和方式。

考虑到以上几点,我将重写您的函数:

代码语言:javascript
复制
ssize_t fgets(FILE *stream, char *buf, size_t size)
{
  if (size == 0)
    return 0;

  size_t count;
  int c = 0;
  for (count = 0; c != '\n' && count < size - 1; count++) 
  {
    c = getc(stream);

    if (c == EOF) {
      if (count == 0)
        return -1;
      break;
    }

    buf[count] = (char) c;
  }

  buf[count] = '\0';
  return (ssize_t) count;
}
票数 6
EN

Code Review用户

发布于 2016-02-07 20:47:43

这里应该有一些错误检查。例如,它应该返回一个ssize_t,允许它返回一个负值来指示一个错误(任何在0或0以上的东西都可以看作是一个有效的计数)。它也是实际功能的返回类型。

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/119219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档