处理您在 C 中尝试执行的操作的标准方法是,将未知数量的行(长度未知)读取到 quote 行的“数组”中,以分配一些合理预期的指针数量(使用malloc 或calloc),为每一行读取/分配存储,将行分配给一个空指针并重复直到达到指针的限制。当您达到原始指针限制时,只需 realloc 将您必须的指针数量增加到当前的 2 倍并继续前进。
然后您可以根据需要解析该行。在您的情况下,将每一行转换为 32 位值。要读取每一行,C 中line-oriented 输入的标准工具是fgets 和getline。由于您从 C++ 中提到了getline,因此不相关的 C 函数使用如下所示。 getline 将分配足够的空间来容纳读取的每一行数据。但是,它重用了自己的缓冲区,因此您需要为每一行分配存储空间并复制getline 返回的行。 (strdup 可以在一次通话中完成这两项工作)。
查看以下内容,如果您有任何问题,请告诉我。 注意:enum 仅用于定义分配的初始指针数量的常量。你可以对#define MAXL 64 做同样的事情。另请注意,行索引“idx”作为指向readfile 函数的指针传递,因此当readfile 返回时,读取的行数可以在main 中返回:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
enum { MAXL = 64 };
char **readfile (FILE *fp, size_t *idx);
void *xcalloc (size_t n, size_t s);
void *xrealloc_dp (void *ptr, size_t *n);
FILE *xfopen (const char *fn, const char *mode);
int main (int argc, char **argv) {
char **iflines = NULL;
size_t i, idx = 0;
FILE *fp = argc > 1 ? xfopen (argv[1], "r") : stdin;
/* read file into dynamically allocated 'iflines' */
if (!(iflines = readfile (fp, &idx))) return 1;
if (fp != stdin) fclose (fp); /* close file */
for (i = 0; i < idx; i++) /* print lines */
printf (" line[%2zu] : %s\n", i, iflines[i]);
for (i = 0; i < idx; i++) /* free allocated memory */
free (iflines[i]);
free (iflines);
return 0;
}
/* return allocated pointer to array of pointers containing
* lines from 'fp'
*/
char **readfile (FILE *fp, size_t *idx)
{
if (!fp) return NULL;
char **filebuf = NULL;
char *line = NULL;
size_t maxl = MAXL, n = 0;
ssize_t nchr = 0;
/* allocate MAXL pointers */
filebuf = xcalloc (MAXL, sizeof *filebuf);
while ((nchr = getline (&line, &n, fp)) != -1)
{ /* trim trailing newline or carriage return */
while (nchr && (line[nchr-1] == '\n' || line[nchr-1] == '\r'))
line[--nchr] = 0;
filebuf[(*idx)++] = strdup (line); /* allocate & copy */
/* realloc as required */
if (*idx == maxl) filebuf = xrealloc_dp (filebuf, &maxl);
}
free (line); /* free getline allocated memory */
return filebuf;
}
void *xcalloc (size_t n, size_t s)
{
register void *memptr = calloc (n, s);
if (memptr == 0) {
fprintf (stderr, "xcalloc() error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
return memptr;
}
void *xrealloc_dp (void *ptr, size_t *n)
{
void **p = ptr;
void *tmp = realloc (p, 2 * *n * sizeof tmp);
if (!tmp) {
fprintf (stderr, "xrealloc_dp() error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
p = tmp;
memset (p + *n, 0, *n * sizeof tmp); /* set new pointers NULL */
*n *= 2;
return p;
}
FILE *xfopen (const char *fn, const char *mode)
{
FILE *fp = fopen (fn, mode);
if (!fp) {
fprintf (stderr, "xfopen() error: file open failed '%s'.\n", fn);
// return NULL; /* return or exit as desired */
exit (EXIT_FAILURE);
}
return fp;
}
示例输入
$ cat dat/10int_nl.txt
8572
-2213
6434
16330
3034
12346
4855
16985
11250
1495
输出
$ ./bin/getline_min_fn dat/10int_nl.txt
line[ 0] : 8572
line[ 1] : -2213
line[ 2] : 6434
line[ 3] : 16330
line[ 4] : 3034
line[ 5] : 12346
line[ 6] : 4855
line[ 7] : 16985
line[ 8] : 11250
line[ 9] : 1495
注意: xcalloc、xrealloc_dp 和 xfopen 只是辅助函数,它们对各自的函数 calloc、realloc 和 fopen 进行适当的错误检查。 xrealloc_dp 名称只是表明它正在重新分配一个 pointer-to-pointer-to-type(通常称为 double-pointer)。因此,xrealloc_dp 名称。
仔细阅读,如果您有任何问题,或者我误解了您的问题,请告诉我。