C:如何将不同大小的文本文件的每一行存储到 int 数组中?

标签 c arrays dynamic multidimensional-array fgets

我需要读取一个文件并在每个时间步将每一行(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);

}

感谢大家对我的帮助。

最佳答案

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

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);  
}

关于C:如何将不同大小的文本文件的每一行存储到 int 数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23393067/

相关文章:

c - 错误的 Malloc() 三层结构?

android - 使用通知计数指示器制作 "badge"自定义 View

c - 是否使用未初始化的值调用 `wait` 未定义行为?

c - C 中的哈希表问题;在 tcache 2 中检测到双重释放

c - 并行归并排序(使用fork)

java - 从类名数组创建类

c - GCC:在每条指令之后强制调用函数(用于多线程测试)?

c - 从 C 中的线程数组返回?

c# - 在动态对象上调用方法基本上都是 GetMethod().Invoke() 吗?