- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用 getline
函数从 STDIN
中读取一行。
getline
的原型(prototype)是:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
我将其用作测试程序,它来自 http://www.crasseux.com/books/ctutorial/getline.html#getline
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int atgc, char *argv[])
{
int bytes_read = 1;
int nbytes = 10;
char *my_string;
my_string = (char *)malloc(nbytes+1);
puts("Please enter a line of text");
bytes_read = getline(&my_string, &nbytes, stdin);
if (bytes_read == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");
puts (my_string);
}
return 0;
}
这很好用。
我的疑问是?
为什么使用 char **lineptr
而不是 char *lineptr
作为函数 getline
的参数?
为什么我用下面的代码会出错:
char **my_string;
bytes_read = getline(my_string, &nbytes, stdin);
我对 *
和 &
感到困惑。
部分警告:
testGetline.c: In function ‘main’:
testGetline.c:34: warning: pointer targets in passing argument 2 of
‘getline’ differ in signedness
/usr/include/stdio.h:671:
note: expected ‘size_t * __restrict__’ but argument is of type ‘int *’
testGetline.c:40: warning: passing argument 1 of ‘putchar’ makes integer
from pointer without a cast
/usr/include/stdio.h:582: note: expected ‘int’ but argument is of
type ‘char *’
我使用 GCC 版本 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)。
最佳答案
Why use
char **lineptr
instead ofchar *lineptr
as a parameter of functiongetline
?
想象一下 getline
的原型(prototype)看起来像这样:
ssize_t
getline(char *line, size_t n, FILE *stream);
你这样调用它:
char *buffer = NULL;
size_t len = 0;
ssize_t read = getline(buffer, len, stdin);
在调用getline
之前,buffer
为空:
+------+
|buffer+-------> NULL
+------+
当 getline
被调用时,line
获取 buffer
的副本,因为函数参数在 C 中是按值传递的。 getline 内部
,我们无法再访问 buffer
:
+------+
|buffer+-------> NULL
+------+ ^
|
+------+ |
| line +----------+
+------+
getline
使用 malloc
分配一些内存并将 line
指向 block 的开头:
+------+
|buffer+-------> NULL
+------+
+------+ +---+---+---+---+---+
| line +------->+ | | | | |
+------+ +---+---+---+---+---+
在 getline
返回后,我们无法再访问 line
:
+------+
|buffer+-------> NULL
+------+
我们又回到了起点。我们不能将 buffer
重新指向 getline
中新分配的内存,因为我们只有 buffer
的副本。
getline
的原型(prototype)实际上是:
ssize_t
getline(char **lineptr, size_t *n, FILE *stream);
你这样调用它:
char *buffer = NULL;
size_t len = 0;
ssize_t read = getline(&buffer, &len, stdin);
&buffer
返回指向 buffer
的指针,所以我们有:
+-------+ +------+
|&buffer+------> +buffer+-------> NULL
+-------+ +---+--+
当 getline
被调用时,lineptr
得到 &buffer
的副本,因为 C 是按值调用的。 lineptr
指向与&buffer
相同的地方:
+-------+ +------+
|&buffer+------->+buffer+-------> NULL
+-------+ +---+--+
^
+-------+ |
|lineptr+------------+
+-------+
getline
使用 malloc
分配一些内存并指向 lineptr
的指针(即 lineptr
指向的东西) 在 block 的开头:
+-------+ +------+ +---+---+---+---+---+
|&buffer+------->+buffer+------->+ | | | | |
+-------+ +---+--+ +---+---+---+---+---+
^
+-------+ |
|lineptr+------------+
+-------+
在 getline
返回后,我们无法再访问 lineptr
,但我们仍然可以通过 buffer
访问新分配的内存:
+-------+ +------+ +---+---+---+---+---+
|&buffer+------->+buffer+------->+ | | | | |
+-------+ +---+--+ +---+---+---+---+---+
关于c - 为什么 getline 的第一个参数是指向指针 "char**"而不是 "char*"的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5744393/
main = do input [f a] -> f [a] sequenceA [] = pure [] sequenceA (x:xs) = (:) x sequenceA xs 很
while(getline()) 和 while(!getline().eof()) 有什么区别? 正在解析输入字符串。我尝试了两种条件检查,结果有所不同。 std::string testStr =
我知道我知道。以前有人问过这个问题,但我查看了所有答案,但似乎没有一个能解决我的问题。当我使用 getline 函数获取文件中一行的内容时,它不起作用。 getline(file, line); '文
使用 getline() 函数时出现以下错误: 没有重载函数“getline”的实例匹配参数列表 在一个名为“Time”的类中,我在读取以下输入时使用它: istream & operator >>
我正在阅读 C++ 入门书并尝试所有代码示例。我对这个很感兴趣: #include #include using std::string; using std::cin; using std::c
#include #include #include using namespace std; int main() { char d,a[9],e[9]; cin.getline(a,9); c
假设我想从控制台读取一个整数,并且我不希望程序在输入非整数字符时中断。这就是我的做法: #include #include #include using namespace std; int m
这可能不是一个错误,但我不知道出了什么问题。我的第一个条目在第二次迭代中对 str1 重复,并且从那时起也是如此。只有第一次迭代顺利。 #include #include using namesp
string text; getline(text.c_str(),256); 1) 我收到错误消息“错误:没有匹配函数来调用‘getline(const char*, int)”上面有什么问题,因为
哪个更好,更受欢迎?我真的发现阅读 API 令人困惑。 最佳答案 成员(member)版读入char*,免费版读入std::string。所以更喜欢免费版!像这样使用它: std::istream &
我正在尝试解决这个问题,但出于某种原因,我一直收到这个问题: no instance of "getline" matches the argument list. 我查过这个问题,很多次是因为人们使
长话短说 使用 libc++ 版本的 getline 函数的程序在从管道读取输入时会阻塞,直到管道的缓冲区已满。 NOT libstdc++ 版本的 getline 函数也是如此:这里函数立即读取并返
我的程序的一部分: #include #include #include #include #include using namespace std; /* Works for istrin
我是一名正在准备期末考试的 C++ 初学者。我用两种方式写了一个程序。第一个代码使用 cin.getline() 并且不能正常工作。第二个代码使用 cin.get() 和 cin >> 并正确执行所有
我正在尝试使用 getline 解析文档以获取整行并将其放入名为“line”的字符串变量中。问题是我收到一条错误消息:“没有重载函数 getline 的实例与参数列表匹配。”谁能帮我解决这个问题? #
我有一个带有 getline 函数的源代码文件,当我编译它时收到错误(下面的代码和错误)。我的问题是我从一个已经编译和工作的程序(也包括在下面)中复制并粘贴了整个函数。我在程序的其他 2 个源代码文件
我正在尝试从文件中读取,但 C++ 不想运行 getline()。 我收到这个错误: C:\main.cpp:18: error: no matching function for call to '
例子: std::ifstream in("some_file.txt"); std::string line; // must be outside ? while(getline(in,line)
注意:已解决,问题不是 getline() 而是 find 函数数组填充不当! 在发布我自己的问题之前,我已经查找了几个问题,但找不到我的问题的答案。这是我发布的第一个问题,但在发布我自己的问题之前,
我的代码块中有一个 getline 函数。但是在编译 make 文件时出现以下错误: cc -DMAIN -c -o terp.o terp.c terp.c:130:15: error: con
我是一名优秀的程序员,十分优秀!