C 中合理的行缓冲区大小?

发布于 2024-09-16 07:47:39 字数 439 浏览 8 评论 0 原文

我正在使用 popen 读取 shell 命令的输出。我将使用 fgets 逐行读取。 我的问题是如何为我的 char* 缓冲区选择最佳缓冲区大小?我记得一位教授告诉我们要包含 并使用 LINE_MAX 来处理此类事情。它在我的 Mac 上运行良好,但在 Linux 上没有 LINE_MAX

这个邮件列表存档提出了同样的问题,但没有回答我的问题 http://bytes.com/topic/ c/answers/843278-not-able-locate-line_max-limits-h

I'm using popen to read output from shell commands. I will use fgets to read line by line.
My question is how to choose the best buffer size for my char* buffer? I remember from a professor telling us to include <limits.h> and use LINE_MAX for such things. It works fine on my Mac, but there's no LINE_MAX on Linux.

This mailing list archive poses the same question, but no answer to my question
http://bytes.com/topic/c/answers/843278-not-able-locate-line_max-limits-h

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

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

发布评论

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

评论(5

多情癖 2024-09-23 07:47:39

没有定义LINE_MAX,看看_POSIX2_LINE_MAX,要求至少是2048。我通常使用4096。

另外寻找(新的)POSIX函数getline()getdelim( ) - 两者位于同一 URL。这些根据需要分配内存。


程序 (posix2_line_max.c)

#include "posixver.h"
#include <limits.h>
#include <stdio.h>

int main(void)
{
  printf("%d\n", _POSIX2_LINE_MAX);
  return 0;
}

输出:

2048

posixver.h

#ifndef JLSS_ID_POSIXVER_H
#define JLSS_ID_POSIXVER_H

/*
** Include this file before including system headers.  By default, with
** C99 support from the compiler, it requests POSIX 2001 support.  With
** C89 support only, it requests POSIX 1997 support.  Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600   /* SUS v3, POSIX 1003.1 2004 (POSIX 2001 + Corrigenda) */
#else
#define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

#endif /* JLSS_ID_POSIXVER_H */

在 Ubuntu 12.04 衍生版本上测试;命令行:

gcc -g -O3 -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Werror  posix2_line_max.c -o posix2_line_max

When <limits.h> does not define LINE_MAX, look at _POSIX2_LINE_MAX, which is required to be at least 2048. I usually use 4096.

Also look for the (new) POSIX functions getline() and getdelim() - both at the same URL. These allocate memory as necessary.


Program (posix2_line_max.c)

#include "posixver.h"
#include <limits.h>
#include <stdio.h>

int main(void)
{
  printf("%d\n", _POSIX2_LINE_MAX);
  return 0;
}

Output:

2048

posixver.h

#ifndef JLSS_ID_POSIXVER_H
#define JLSS_ID_POSIXVER_H

/*
** Include this file before including system headers.  By default, with
** C99 support from the compiler, it requests POSIX 2001 support.  With
** C89 support only, it requests POSIX 1997 support.  Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600   /* SUS v3, POSIX 1003.1 2004 (POSIX 2001 + Corrigenda) */
#else
#define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

#endif /* JLSS_ID_POSIXVER_H */

Tested on an Ubuntu 12.04 derivative; command line:

gcc -g -O3 -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Werror  posix2_line_max.c -o posix2_line_max
诠释孤独 2024-09-23 07:47:39

man getline

另请参阅 http:// www.gnu.org/s/libc/manual/html_node/Line-Input.html 以及 getline()fgets() 对比的讨论.获取()。受到如此话题影响的次数超出了我的统计范围。

man getline

Also see http://www.gnu.org/s/libc/manual/html_node/Line-Input.html and the discussion of getline() vs. fgets() vs. gets(). Has been subject on SO more often than I can count as well.

゛时过境迁 2024-09-23 07:47:39

您可以使用 malloc() 并根据需要进行扩展,或者使用源代码并查看 GNU 实用程序如何执行此操作。

You could use malloc() and expand if necessary, or use the source and look at how a GNU utility does it.

冧九 2024-09-23 07:47:39

检查该行是否有“\n”,如果不存在,请在调用下一个 fget 之前扩展缓冲区。

check the line for an '\n', if not exists expand the buffer before you call the next fgets.

没企图 2024-09-23 07:47:39

POSIX 系统有 getline ,它将为您分配一个缓冲区。

在非 POSIX 系统上,您可以使用 Chuck B. Falconer 的公共域 ggets 函数,该函数类似。 (Chuck Falconer 的网站不再可用,尽管 archive.org 有一个副本,我已经为 我自己的页面>ggets。)

POSIX systems have getline which will allocate a buffer for you.

On non-POSIX systems, you can use Chuck B. Falconer's public domain ggets function, which is similar. (Chuck Falconer's website is no longer available, although archive.org has a copy, and I've made my own page for ggets.)

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