【问题标题】:C: how to store into a int array each line of a text file with different size?C:如何将不同大小的文本文件的每一行存储到一个int数组中?
【发布时间】:2014-04-30 16:11:26
【问题描述】:

我需要读取一个文件并在每个时间步将每一行 (int) 存储在一个整数数组中,然后在这个数组中工作。

输入看起来像这样:

0 16 12

1 10 17

2 14 8

3 12 17 16

9 14 16 19 13 5

19 16 6 17 11 15 9 4 12 18 8

然后使用这样的东西我可以读取和打印每一行,但我不能每次将每一行保存在一个数组中。

char matrix[500][500], space;
int numbers[500], i = 0, j;
int elementsA[10000];
FILE *fp = fopen("mygraph", "r");
int blabla[1000000];
int a;

while(!feof(fp))
{
    fscanf(fp, "%d", &numbers[i]); // getting the number at the beggining
    fscanf(fp, "%c", &space); // getting the empty space after the number
    fgets(matrix[i++], 500, fp); //getting the string after a number        
    a ++;
}
for(j = 0; j < i; j++)
    printf("%d %s %d\n", numbers[j], matrix[j]);



return(0);

}

感谢大家帮助我。

【问题讨论】:

  • 你为什么不一次读一整行,然后解析它。有一个数组数组,每个子数组的大小都是动态的。每个数组中的第一个元素应该是后续元素的数量。
  • 这就是我想做的,但我不知道该怎么做?
  • 对不起,这不是代码编写服务。
  • @user3590067 - 请参阅下文了解您的问题的一些答案。
  • @OldProgrammer - 每个数组中的第一个元素应该是后续元素的数量...为什么?

标签: c arrays dynamic multidimensional-array fgets


【解决方案1】:

一种可能的方法使用以下步骤:

1) 获取文件指针:FILE *fp = fopen("c:\\dir1\\file.txt", "r");
2) 使用 fgets()有两种方式:
首先计算文件中的行数,并获得最长的行(提示:计算整数个数的空格)
(使用此信息,您可以为 2D int 数组创建和分配内存)
从头开始重新开始,一次读取一行。
3) 使用 strtok()(或 strtok_r())将每一行解析为整数并放入您的数组中
4) 释放所有内存并退出。

这是一种获取分配数组的信息的方法:
提供输入(每一行都必须包含一个 \n,就像这个一样) :

0 16 12
1 10 17
2 14 8
3 12 17 16
9 14 16 19 13 5
19 16 6 17 11 15 9 4 12 18 8  

以下代码将提供文件中的行数和最大整数/行数,
然后创建你的数组:

int GetParamsOfFile(char *path, int *numInts) ;

int main(void)
{
    int lines;
    int mostInts;
    int **array=0;
    lines = GetParamsOfFile("place your path here", &mostInts);
    //                      rows   columns (will correlate with rows and column of your file)
    array = Create2D(array, lines, mostInts);//creates array with correct number of rows and columms;
    //Here is where you would re-read your file, this time, populating your new array.  
    //I will leave this part to you... 
    free2DInt(array, mostInts);
    return 0;
}

int GetParamsOfFile(char *path, int *numInts)
{
    FILE *fp = fopen(path, "r");
    int spaces=0, lines=0, sK=0;
    char *line;  //choose big here
    char *buf;
    line = malloc(1000);
    line = fgets(line, 1000, fp);
    while(line)
    {
        lines++;
        spaces = 0;
        buf = strtok(line, " ");
        while(buf)
        {
            spaces++;
            buf = strtok(NULL, " ");
        }
        (spaces > sK)?(sK = spaces):(sK==sK);
        line = fgets(line, 1000, fp);
    }
    *numInts = sK;
    fclose(fp);
    free(line);
    return lines;
}

int ** Create2D(int **arr, int rows, int cols)
{   
    int space = cols*rows; 
    int    y;

    arr   = calloc(space, sizeof(int));
    for(y=0;y<rows;y++)
    {
        arr[y] = calloc(cols, sizeof(int)); 
    }
    return arr;
}

void free2DInt(int **arr, int rows)
{
    int i;
    for(i=0;i<rows; i++)
        if(arr[i]) free(arr[i]);
    free(arr);  
}

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    #include <ctype.h>
    
    int countitems(char * s) {
      int i;
    
      for (i=0; *s; i++) {
        while (*s && isblank(*s++));
        while (*s && !isblank(*s++));    
      }
      return(i);
    }
    
    char * nextitem(char *s) {
      while (*s && !isblank(*s)) s++;
      while (*s && isblank(*s)) s++;
      return (s);
    }
    
    char * skipblanks(char *s) {
      while (*s && isblank(*s)) s++;
    
      return (s);
    }
    
    int main() {
      FILE *f;
      int i, j;
      char s[100];
      char * ss;
    
      f = fopen("mygraph.txt", "r");
      i = 0;
      while (!feof(f)) {
        fgets(s, 99, f);
        j = countitems(s);
        if (j>i) i = j;
      }
      printf("you need %d columns\n", i);
      // allocate matrix
      rewind(f); // at leat once in a life...rewind!!
      while(!feof(f)) {
        fgets(s, 99, f);
        ss = skipblanks(s);
        while (*ss) {
          sscanf(ss, "%d", &j);
          printf("%d ", j); // do something with numbers
          ss = nextitem(ss);
        }
        printf("\n");
      }
      fclose(f);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-02
      • 1970-01-01
      • 2016-03-04
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2020-01-24
      • 1970-01-01
      相关资源
      最近更新 更多