如何在char*
不指定大小的情况下将无限字符读入变量?
例如,假设我想读取可能也需要多行的员工的地址。
您必须首先“猜测”您期望的大小,然后使用malloc
. 如果结果太小,您可以使用realloc
将缓冲区调整为更大一点。示例代码:
char *buffer;
size_t num_read;
size_t buffer_size;
buffer_size = 100;
buffer = malloc(buffer_size);
num_read = 0;
while (!finished_reading()) {
char c = getchar();
if (num_read >= buffer_size) {
char *new_buffer;
buffer_size *= 2; // try a buffer that's twice as big as before
new_buffer = realloc(buffer, buffer_size);
if (new_buffer == NULL) {
free(buffer);
/* Abort - out of memory */
}
buffer = new_buffer;
}
buffer[num_read] = c;
num_read++;
}
这只是我的想法,可能(阅读:可能)包含错误,但应该给你一个好主意。
只需要回答 Ivor Horton 第 3 版的《Beginning C》第 330 页的 Ex7.1。花了几个星期来锻炼。允许输入浮点数,而无需预先指定用户将输入多少个数字。将数字存储在动态数组中,然后打印出数字和平均值。在 Ubuntu 11.04 中使用 Code::Blocks。希望能帮助到你。
/*realloc_for_averaging_value_of_floats_fri14Sept2012_16:30 */
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
int main(int argc, char ** argv[])
{
float input = 0;
int count=0, n = 0;
float *numbers = NULL;
float *more_numbers;
float sum = 0.0;
while (TRUE)
{
do
{
printf("Enter an floating point value (0 to end): ");
scanf("%f", &input);
count++;
more_numbers = (float*) realloc(numbers, count * sizeof(float));
if ( more_numbers != NULL )
{
numbers = more_numbers;
numbers[count - 1] = input;
}
else
{
free(numbers);
puts("Error (re)allocating memory");
exit(TRUE);
}
} while ( input != 0 );
printf("Numbers entered: ");
while( n < count )
{
printf("%f ", numbers[n]); /* n is always less than count.*/
n++;
}
/*need n++ otherwise loops forever*/
n = 0;
while( n < count )
{
sum += numbers[n]; /*Add numbers together*/
n++;
}
/* Divide sum / count = average.*/
printf("\n Average of floats = %f \n", sum / (count - 1));
}
return 0;
}
/* Success Fri Sept 14 13:29 . That was hard work.*/
/* Always looks simple when working.*/
/* Next step is to use a function to work out the average.*/
/*Anonymous on July 04, 2012*/
/* http://www.careercup.com/question?id=14193663 */
将一个 1KB 缓冲区(或 4KB)放在堆栈上,读取它直到找到地址的结尾,然后分配一个正确大小的缓冲区并将数据复制到它,怎么样?一旦你从函数返回,堆栈缓冲区就会消失,你只需要一次调用malloc
.