如何将 getline() 的每个字符串存储在(动态)字符串数组中?
How store each string of getline() inside a (dynamic) array of strings?
我正在使用 getline()
函数获取 stdin
的每一行。每行都是一个不同长度的字符串:
#include <stdio.h>
#include <stdlib.h>
int main() {
char *line = NULL;
size_t foo = 0;
ssize_t reader;
while ((reader = getline(&line, &foo, stdin)) != -1) { // %zu of reader is length of line
printf("%s", line);
}
free(line);
return 0;
}
在每次迭代中,line
是一个字符串并且包含当前行。如何获取每个字符串行并将其存储在数组中?我尝试了几种方法,但其中 none 起作用了,或者它们只是导致内存访问失败:(
我希望我的问题很清楚?如果不是,请告诉我,我会改!
除非您事先知道预期有多少行,否则您将不得不动态分配数组,例如:
#include <stdio.h>
#include <stdlib.h>
int main() {
char *line = NULL;
size_t foo = 0;
ssize_t reader;
int result = 0;
int numlines = 0, maxlines = 10;
char **lines = malloc(sizeof(char*) * maxlines);
if (!lines) {
printf("error allocating array\n");
}
else {
while ((reader = getline(&line, &foo, stdin)) != -1) { // %zu of reader is length of line
printf("%s", line);
if (numlines == maxlines) {
maxlines *= 2; // <-- or use whatever threshold makes sense for you
char **newlines = realloc(lines, sizeof(char*) * maxlines);
if (!newlines) {
printf("error reallocating array\n");
result = -1;
break;
}
lines = newlines;
}
lines[numlines] = line;
++numlines;
line = NULL;
foo = 0;
}
free(line); // <-- in case getline() or realloc() failed...
// use lines up to numlines as needed
// free lines
for(int i = 0; i < numlines; ++i) {
free(lines[i]);
}
free(lines);
}
return result;
}
您需要创建一个在需要时调整大小的指针数组:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// start with an array that ends with a NULL pointer
// (like argv does)
size_t numLines = 0;
char **lines = malloc( ( numLines + 1 ) * sizeof( *lines ) );
lines[ numLines ] = NULL;
// break the loop explicitly - easier to handle and much less
// bug-prone than putting the assignment into a while statement
for ( ;; )
{
// get the next line
size_t bytes = 0UL;
char *line = NULL;
ssize_t result = getline( &line, &bytes, stdin );
if ( result < 0 )
{
break;
}
// enlarge the array by one
numLines++;
char **tmp = realloc( lines, ( numLines + 1 ) * sizeof( *tmp ) );
if ( !tmp )
{
break;
}
lines = tmp;
// add the new line to the end of the array
lines[ numLines ] = line;
lines[ numLines + 1 ] = NULL;
}
// use lines - then free them
return( 0 );
}
这可以通过分块执行 realloc()
调用来优化,例如每 32 或 64 行。但是鉴于您已经有效地每行调用一次 malloc()
,这可能没有多大帮助。
我正在使用 getline()
函数获取 stdin
的每一行。每行都是一个不同长度的字符串:
#include <stdio.h>
#include <stdlib.h>
int main() {
char *line = NULL;
size_t foo = 0;
ssize_t reader;
while ((reader = getline(&line, &foo, stdin)) != -1) { // %zu of reader is length of line
printf("%s", line);
}
free(line);
return 0;
}
在每次迭代中,line
是一个字符串并且包含当前行。如何获取每个字符串行并将其存储在数组中?我尝试了几种方法,但其中 none 起作用了,或者它们只是导致内存访问失败:(
我希望我的问题很清楚?如果不是,请告诉我,我会改!
除非您事先知道预期有多少行,否则您将不得不动态分配数组,例如:
#include <stdio.h>
#include <stdlib.h>
int main() {
char *line = NULL;
size_t foo = 0;
ssize_t reader;
int result = 0;
int numlines = 0, maxlines = 10;
char **lines = malloc(sizeof(char*) * maxlines);
if (!lines) {
printf("error allocating array\n");
}
else {
while ((reader = getline(&line, &foo, stdin)) != -1) { // %zu of reader is length of line
printf("%s", line);
if (numlines == maxlines) {
maxlines *= 2; // <-- or use whatever threshold makes sense for you
char **newlines = realloc(lines, sizeof(char*) * maxlines);
if (!newlines) {
printf("error reallocating array\n");
result = -1;
break;
}
lines = newlines;
}
lines[numlines] = line;
++numlines;
line = NULL;
foo = 0;
}
free(line); // <-- in case getline() or realloc() failed...
// use lines up to numlines as needed
// free lines
for(int i = 0; i < numlines; ++i) {
free(lines[i]);
}
free(lines);
}
return result;
}
您需要创建一个在需要时调整大小的指针数组:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// start with an array that ends with a NULL pointer
// (like argv does)
size_t numLines = 0;
char **lines = malloc( ( numLines + 1 ) * sizeof( *lines ) );
lines[ numLines ] = NULL;
// break the loop explicitly - easier to handle and much less
// bug-prone than putting the assignment into a while statement
for ( ;; )
{
// get the next line
size_t bytes = 0UL;
char *line = NULL;
ssize_t result = getline( &line, &bytes, stdin );
if ( result < 0 )
{
break;
}
// enlarge the array by one
numLines++;
char **tmp = realloc( lines, ( numLines + 1 ) * sizeof( *tmp ) );
if ( !tmp )
{
break;
}
lines = tmp;
// add the new line to the end of the array
lines[ numLines ] = line;
lines[ numLines + 1 ] = NULL;
}
// use lines - then free them
return( 0 );
}
这可以通过分块执行 realloc()
调用来优化,例如每 32 或 64 行。但是鉴于您已经有效地每行调用一次 malloc()
,这可能没有多大帮助。