0

我有一个我正在尝试编写的 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.

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

4

2 回答 2

3

滥用buffer

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

您将不得不复制读取的行,可能使用strdup()

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

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

你能澄清一下为什么会这样吗?fgets()存在的声明char *fgets(),我的印象是它暂时返回了一个指向当前从文件中读取的行的指针!

实际上,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;这就是错误输出通道的用途。

于 2013-01-22T05:54:47.660 回答
1

在您的代码中,您正在使 str 数组的每个索引指向相同的数组缓冲区。这就是为什么您为 str 数组中的每个索引获得相同的值(缓冲区中的值)。您可以像这样使用数组使用代码

#define MAX_LINES 100
#define MAX_CHAR 80

int main(int argc, char *argv[])
{
        if(argc != 2)
                 goto out;
         FILE *fp;
         char str[MAX_LINES][MAX_CHAR];
         char buffer[MAX_CHAR];
         char *revstr[MAX_CHAR];
         int curr_line = 0, i = 0, j =0;

         fp = fopen(argv[1],"r");

         out:
         if (fp == NULL && argc != 2){
                 printf("File cannot be found or read failed.\n");
                 return -1;
         }

         /* 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"); */

         /* fgets reads one line at a time, until 80 chars, EOL or EOF */
         while(curr_line < MAX_LINES && ((fgets(buffer, MAX_CHAR, fp)) != NULL)){

                 //str[i] = buffer;
                 memcpy(str[i], buffer, strlen(buffer));
                 curr_line++;
                 ++j;
                 ++i;
         }

         for(i = 0; i < 4; ++i) 
                 printf("%s \n", str[i]);


         printf("END OF PROGRAM RUN.");
         return 0;
 }

你也没有修改变量 currn_line

于 2013-01-22T06:24:47.667 回答