gpt4 book ai didi

c - 使用 fgets 将行复制到字符串数组中?

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:28 27 4
gpt4 key购买 nike

我有一个我正在尝试编写的 C 程序,它打算反转文件的行。我在 C 方面仍然很无能(尽管我来自 Java 背景),所以我很可能会在指针等方面犯错误,但我已经尝试每次都查阅手册。这有点像是一项任务。

程序的要点是反转文件的内容,最多 MAX_LINES 行,每行不超过 MAX_CHARS。我想尝试的方法如下:使用 fgets 从文件中读取 80 个字符或直到 EOL,存储该字符串,然后重复该过程直到达到 EOF 或 MAX_LINES,使用旁边的计数器。之后我只是将相同的字符串放在不同的数组中,从 second_array[counter] 到 0。但是,我在实际将字符串放入第一个数组时遇到了问题。这是我到目前为止所拥有的:

1 #include <stdio.h>
2 #include <string.h>
3
4 #define MAX_LINES 100
5 #define MAX_CHAR 80
6
7 int main(int argc, char *argv[])
8 {
9 if(argc != 2)
10 goto out;
11 FILE *fp;
12 char *str[MAX_CHAR];
13 char buffer[MAX_CHAR];
14 char *revstr[MAX_CHAR];
15 int curr_line = 0, i = 0, j =0;
16
17 fp = fopen(argv[1],"r");
18
19 out:
20 if (fp == NULL && argc != 2){
21 printf("File cannot be found or read failed.\n");
22 return -1;
23 }
24
25 /* printf("This part of the program reverses the input text file, up to a maximum of 100 lines and 80 characters per line.\n The reversed file, from the last (or 100th) line to the first, is the following:\n\n"); */
26
27 /* fgets reads one line at a time, until 80 chars, EOL or EOF */
28 while(curr_line < MAX_LINES && ((fgets(buffer, MAX_CHAR, fp)) != NULL)){
29
30 str[i] = buffer;
31 ++j;
32 ++i;
33 }
34
35 for(i = 0; i < 4; ++i)
36 printf("%s \n", str[i]);
37
38
39 printf("END OF PROGRAM RUN.");
40 return 0;
41 }

在同一目录中,我有一个包含以下行的“txt”文件:

is this a test
this is a test
this is not a test

当我编译并运行程序时 (./a.out txt),我得到以下输出:

this is not a test

this is not a test

this is not a test

END OF PROGRAM RUN.

显然这意味着它正在覆盖相同的位置,但我不确定如何纠正这个问题(如前所述,指针对我来说仍然很陌生)。谁能澄清这里发生了什么?我需要改用二维数组吗?任何帮助将不胜感激。

最佳答案

滥用buffer

问题是你一直在str[i]中存储指针buffer,但是buffer只包含读取的最后一行。

您必须复制读取的行,可能使用 strdup():

str[i++] = strdup(buffer);

需要担心错误检查,但从概念上讲,这是处理错误的一种简单方法。

Could you please clarify as to why that is? The declaration of fgets() being char *fgets(), I got the impression that it momentarily returns a pointer to the line currently read from the file!

实际上,fgets() 返回两个值之一:

  • EOF 或其他 I/O 错误时为 NULL,或者
  • buffer,指向传递给函数的字符串的指针。

特别是,它不会自动为您创建新的存储空间。如果你想要那个,你可能需要看看 readline()来自 POSIX。

因此,您对 fgets() 的调用每次都使用相同的数组 buffer,因此读取的每个后续行都会覆盖前一行。这就是为什么您需要以某种方式复制每一行,以及为什么我建议使用 strdup() 作为一种简单的方法来做到这一点。


strdup()

strdup() 函数是 POSIX 的一部分,但不是标准 C。但是,它很容易实现:

char *strdup(const char *str)
{
size_t len = strlen(str) + 1;
char *dup = malloc(len);
if (dup != 0)
memmove(dup, str, len);
return(dup);
}

报错

您应该尽可能避免使用 goto,使用这段代码很容易做到这一点。您的双重用途错误消息实际上最终会使省略参数或提供过多参数的用户感到困惑。提供两条单独的错误消息,一条是关于程序的参数数量不正确,另一条是关于未能打开文件。

if (argc != 2)
{
fprintf(stderr, "Usage: %s file\n", argv[0]);
exit(1);
}
fp = fopen(argv[1], "r");
if (fp == 0)
{
fprintf(stderr, "%s: failed to open file %s (%d: %s)\n",
argv[0], argv[1], errno, strerror(errno));
exit(1);
}

注意应该在stderr 上报告错误,而不是stdout;这就是错误输出 channel 的用途。

关于c - 使用 fgets 将行复制到字符串数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14452335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com