C语言 C readline 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2600528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C readline function
提问by nunos
In an assignment for college it was suggested to use the C readlinefunction in an exercise. I have searched for its reference but still haven't found it. Does it really exist? In which header? Can you please post the link to the reference?
在大学作业中,建议readline在练习中使用 C函数。我已经搜索了它的参考资料,但仍然没有找到。它真的存在吗?在哪个标题中?可以发一下参考文献的链接吗?
回答by Tim Post
Readline exists in two places, libreadlineand libedit(also called libeditline). Both have an identical interface. The difference is libreadline is licensed under the GPL, libedit is 3 clause BSD. Licensing is really not a concern for an assignment, at least I don't think it is. Either license allows you to use the code freely. If you link against readline, be sure to make the whole program GPL 2 or laterwhich will satisfy whatever version of the GPL governs the system readline. It may be GPL2+ or GPL3+, depending on the age of the system. I'm not advocating either license, that's up to you.
Readline 存在于两个地方,libreadline并且libedit(也称为libeditline)。两者都有相同的接口。不同之处在于 libreadline 是根据 GPL 许可的,libedit 是 3 条款 BSD。许可确实不是任务的问题,至少我不认为是。任一许可证都允许您自由使用代码。如果您针对 readline 进行链接,请确保使整个程序GPL 2 or later能够满足管理系统的任何 GPL 版本readline。它可能是 GPL2+ 或 GPL3+,具体取决于系统的年龄。我不提倡任何一种许可证,这取决于您。
Note, take care to install either / or and adjust linking as needed (-lreadlineor-leditor-leditline). Both are libraries and not a part of the standard C library.
请注意,请注意安装 / 或并根据需要调整链接(-lreadline或-ledit或-leditline)。两者都是库而不是标准 C 库的一部分。
Edit(afterthought):
编辑(事后):
If releasing a program to the wild, its a nice gesture to allow the user to configure it with their readlineof choice. For instance: --with-readlineor --with-libedit, etc. This allows a binary package that conforms to their choice of license, at least as far as readlineis concerned.
如果将程序发布到野外,这是一个很好的姿态,允许用户根据自己readline的选择对其进行配置。例如:--with-readlineor--with-libedit等。这允许符合他们选择的许可证的二进制包,至少readline就相关而言。
Links: Readlineand Edit/Editline.
回答by yassin
I don't think it's a standard function.
我不认为这是一个标准的功能。
I simple implementation would be like this:
我简单的实现是这样的:
char *Readline(char *in) {
char *cptr;
if (cptr = fgets(in, MAX_LINE, stdin)) {
/* kill preceding whitespace but leave \n so we're guaranteed to have something
while(*cptr == ' ' || *cptr == '\t') {
cptr++;
}
return cptr;
} else {
return 0;
}
}
It uses fgets() to read up to MAX_LINE - 1 characters into the buffer 'in'. It strips preceding whitespace and returns a pointer to the first non-whitespace character.
它使用 fgets() 将最多 MAX_LINE - 1 个字符读入缓冲区“in”。它去除前面的空格并返回一个指向第一个非空格字符的指针。
回答by iGeeks
If you need a "readLine()" function, like the readLine() in Java-BufferedReader, you can also freely use my function ?char* get_line(FILE *filePointer)? in "line.h", which I wrote just for this purpose: https://github.com/pheek/line.h/blob/master/line.h
如果你需要一个“readLine()”函数,比如Java-BufferedReader中的readLine(),你也可以自由使用我的函数?char* get_line(FILE *filePointer)? 在“line.h”中,我只是为此目的而写的:https: //github.com/pheek/line.h/blob/master/line.h
回答by Ajay Sharma
Not sure if you tried reading this from the GNU C Library: ssize_t getline (char **lineptr, size_t *n, FILE *stream).
This function reads a line from a file and can even re-allocate more space if needed.
不确定您是否尝试从 GNU C 库中阅读此内容:ssize_t getline (char **lineptr, size_t *n, FILE *stream). 此函数从文件中读取一行,甚至可以根据需要重新分配更多空间。
An example of this is found in the manpage of getline. Below is a copy of it.
在 getline 的联机帮助页中可以找到一个示例。下面是它的副本。
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
exit(EXIT_FAILURE);
}
stream = fopen(argv[1], "r");
if (stream == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
while ((nread = getline(&line, &len, stream)) != -1) {
printf("Retrieved line of length %zu:\n", nread);
fwrite(line, nread, 1, stdout);
}
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
}
回答by Pavel Radzivilovsky
It doesn't exist.
它不存在。
They were mistaken and referred to gets() from stdio.h.
他们被误认为是从 stdio.h 中引用了gets()。
Also this is a very unsafe function due to no maximum size to read parameter, making it immediate security whole (lookup buffer overrun attack). You may use fgets() instead, like the angry comments below suggest.
此外,这是一个非常不安全的函数,因为没有读取参数的最大大小,使其立即安全(查找缓冲区溢出攻击)。您可以使用 fgets() 代替,就像下面的愤怒评论所建议的那样。

