Ç函数getline() - 如何处理缓冲器/如何阅读数量不明的值到数组 [英] C getline() - how to deal with buffers / how to read unknown number of values into array

查看:156
本文介绍了Ç函数getline() - 如何处理缓冲器/如何阅读数量不明的值到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,一些背景:我试图从外部文件整数列表来获得,并把它们放入数组。我使用的函数getline来解析线路输入文件行:

First of all, some background: I'm trying to get in a list of integers from an external file and put them into an array. I am using getline to parse the input file line by line:

int lines = 0;
size_t * inputBuffer = (size_t *) malloc(sizeof(size_t));
char * storage = NULL;

我打电话函数getline像这样:

I am calling getline like so:

getline(&storage, &s, input)

我从函数getline手册页听说,如果你提供了一个为size_t *缓冲区,你可以当它超过字节分配函数getline调整它。我的问题是,你可以用这个缓冲区?它将包含所有你读函数getline()的项目?是更简单这个缓存器读出,或将这些整数成阵列时遍历输入以不同的方式?谢谢!

I heard from the man page on getline that if you provide a size_t * buffer, you can have getline resize it for you when it exceeds the byte allocation. My question is, what can you use this buffer for? Will it contain all of the items that you read with getline()? Is it simpler to read from this buffer, or to traverse the input in a different way when putting these integers into an array? Thanks!

推荐答案

缓冲区将仅包含的最后的你函数getline读行。目的只是把内存管理的努力一点点关闭code。

The buffer will only contain the last line you read with getline. The purpose is just to take a little bit of the effort of managing memory off your code.

会发生什么事,如果你重复调用函数getline ,多次向它传递相同的缓冲区,为缓冲区将扩展到最长行的长度在文件中并留那里。每次通话将与下一行的替换其内容。

What will happen if you repeatedly call getline, passing it the same buffer repeatedly, is that the buffer will expand to the length of the longest line in your file and stay there. Each call will replace its contents with the next line's.

你没有提供它为size_t * ,你给它一个的char *

You're not providing it a size_t*, you're giving it a char*.

这篇关于Ç函数getline() - 如何处理缓冲器/如何阅读数量不明的值到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆