【问题标题】:Why is it not printing the array?为什么不打印数组?
【发布时间】:2014-10-24 22:23:59
【问题描述】:

我想从文件中取出内容并以随机顺序打印出来。现在我只是想从文件中取出内容并将其打印出来。例如现在文件有:

0 Abdelmeged Zane
1 Attri Sonal

它会打印出第二行而不是第一行。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[]){
   FILE *file;
   char string[50];
   char array[100];
   int i = 0;
   char *p;

   file = fopen(argv[1], "r");
   if(file == NULL) {
      perror("Error opening file");
      return(-1);
   }

   printf("\nOriginal Array: \n");
   while(fgets(string, 100, file)!= NULL) {
       printf(string);
       p = &array[i];
       strcpy(p, string);
       i++;
   }

   i = 0;
   printf("\nShuffled Array: \n");
   while(i < 20){
       printf("%c", array[i]);
       i++;
   }

   fclose(file);
   return(0);
}

【问题讨论】:

  • 注意:不能main()返回-1。其他功能 -- 是的,main -- no.

标签: c arrays file pointers


【解决方案1】:

你的代码的问题是你声明了char array[100];,然后在你的while循环的每次迭代中基本上用strcpy(p, string);覆盖你存储在其中的字符串。在这种情况下,不需要使用指针p。您需要做的是分配 multiple 指针来保存 multiple 字符串。正常的方法是分配一些指针(下面有 10 个——参见 MAXLINES),然后在使用每个指针时为每个字符串分配空间。下面是一个简单的例子(注意有很多不同的方法可以做到这一点)。此外,由于您正在分配内存,因此您也有责任释放它(如下所示)。如果您有任何问题,请发表评论。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLINES 10

int main(int argc, char* argv[]){
    FILE *file;
    char string[50];
    char **array = NULL;
    int i = 0;

    if (argc < 2) {
        fprintf (stderr, "Error: insufficient input. Usage: %s <filename>\n", argv[0]);
        return 1;
    }

    file = fopen(argv[1], "r");
    if(file == NULL) {
        perror("Error opening file");
        return 1;
    }

    array = calloc (MAXLINES, sizeof (*array));    /* MAXLINES char pointers */
    if (!array) {
        fprintf (stderr, "Error: calloc pointer allocation failed\n");
        return 1;
    }


    printf("\nOriginal Array: \n");
    while(fgets(string, 100, file)!= NULL) {
        printf("  %s", string);
        array[i] = strdup (string); /* strdup allocates space for string    */
        i++;
    }

    fclose(file);                    /* close it here, you are done with it */

    i = 0;                           /* print array stored in memory        */
    printf("\nShuffled Array: \n");
    while (array[i])
    {
        printf("  %s", array[i]);
        i++;
    }

    i = 0;
    while (array[i])                /* free memory dynamically allocated:   */
    {
        free (array[i]);            /* free array/string memory             */
        i++;
    }
    if (array) free (array);        /* free pointer allocation              */

    return 0;
}

注意:您还应该使用fgets(例如int numlines = i;)保存循环后读取的字符串数。在重置i = 0;之前执行此操作。这样,您就可以方便地参考要在 shuffle 中使用的字符串数量。 (您也可以随时使用while (array[i]) 循环再次计算它们,但为什么呢?

输入文件:

$ cat dat/abde.txt
0 Abdelmeged Zane
1 Attri Sonal

输出:

$ ./bin/shufs dat/abde.txt

Original Array:
  0 Abdelmeged Zane
  1 Attri Sonal

Shuffled Array:
  0 Abdelmeged Zane
  1 Attri Sonal

【讨论】:

    【解决方案2】:

    您正在将读取的字符串复制到第二个 char 数组中,每次将起点偏移一个。所以最后,假设您没有缓冲区溢出,那么您的 array 将保留每行的第一个字符直到最后一个字符,它将完整地保存。

    根据您在研究中所处的位置,可以创建一个静态字符数组来保存每一行,也可以创建一个指针数组和 malloc 您保存的副本。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 2018-10-25
      • 2020-11-09
      • 2017-05-11
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多