创建从在C文件输入的二维阵列 [英] Creating a two dimensional array from a file input in C
问题描述
TL; DR:用二维数组挣扎
我想从一个文本文件的整数列表创建两个二维数组。这样安排是用C
tester.txt包括:
2 1 2 3 4 5 6 7 8
的第一个数字意味着两个阵列具有2行2列,如果它是任何其它数量的列/行的将被重新presented这样
tester.txt应输出继电器如下:
1 2 5 6
3 4 7 8
下面是我的code:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;诠释的main()
{
INT I,J,K;
FILE *为FilePointer;
INT NROWS;
INT大小; 的fputs(输入文件名:标准输出);
fflush(标准输出); 如果(与fgets(文件名,文件名的sizeof,标准输入)!= NULL)
{
字符*换行=和strchr(文件名,'\\ n'); / *搜索换行符* /
如果(新行!= NULL)
{
*换行='\\ 0'; / *改写结尾的换行* /
}
的printf(文件名= \\%s \\的\\ n,文件名);
} 文件指针= FOPEN(文件名,R);
fseek的(文件指针,0,SEEK_END); //谋求结束文件
大小= FTELL(文件指针);
的printf(大小=%d个\\ N,大小);
fseek的(文件指针,0,SEEK_SET); INT holderarray【规格】; 对于(i = 0; I<大小;我++)
的fscanf(文件指针,%D,&安培; holderarray [I]); NROWS = holderarray [0];
的printf(行/列=%d个\\ N多,NROWS); 如果(文件指针== NULL)
{
fprintf中(标准错误,无法打开输入文件in.list \\ n!);
出口(1);
}
}
一切正常,直到这一点。我无法想象如何将价值观上半年添加到新的2维数组,希望你们能帮助。这是我在$ C $个cblocks集思广益。
INT矩阵[NROWS] [NROWS];
INT矩阵2 [NROWS] [NROWS];
对于(i = 1; I<的sizeof(holderarray);我++)
{
为(J = 0; J< NROWS; J ++)
{
矩阵[I] [J] = holderarray [J]。
}对于(i = 0; I<的sizeof(NROWS);我++)
{
为(J = 0; J<的sizeof(NROWS); J ++)
{
的printf(%d个,矩阵[I] [J]);
}}
返回0;
您不能动态申报标准的C数组像这样基于未在编译时已知变量:
INT矩阵[NROWS] [NROWS];
INT矩阵2 [NROWS] [NROWS];
您需要做的,如果你不使用C99或更高版本,是使用的的malloc函数动态分配内存给你:
INT **矩阵,**矩阵2;
INT I;矩阵=的malloc(NROWS * sizeof的为(int *));
矩阵2 =的malloc(NROWS * sizeof的为(int *));对于(i = 0; I< NROWS;我++){
矩阵[I] =的malloc(NROWS *的sizeof(INT));
矩阵2 [I] =的malloc(NROWS *的sizeof(INT));
}
在C两个维数组被视为指针的指针。每个指针是一个连续的内存块包含整数(即你的矩阵的行)的引用,然后我们用一个指针的指针为内存另一个相邻块包含引用/指针的第一个元素的引用在所有这些行。
此图像(不是我)可以帮助:<一href=\"http://3.bp.blogspot.com/-Ce4hgph2lJ4/TsLgdbwtg7I/AAAAAAAABms/JsSceuX4IZo/s1600/array_2d_dynamic.png\"相对=nofollow>动态二维数组
请注意,当你使用它完成这个动态分配的内存应该释放:
的for(int i = 0; I&LT; NROWS;我++){
免费(矩阵[I]);
免费(矩阵2 [I]);
}免费(矩阵);
免费(矩阵2);
TL;DR: Struggling with 2 dimensional arrays.
I'm trying to create two two dimensional array from a list of integers from a text file. This is programmed in C.
tester.txt contains:
2 1 2 3 4 5 6 7 8
The first number means that both arrays have 2 rows and 2 columns, if it were any other number the columns/rows would be represented as such.
tester.txt should ouput the following:
1 2 5 6
3 4 7 8
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i,j,k;
FILE *filepointer;
int nrows;
int size;
fputs("Enter a filename: ", stdout);
fflush(stdout);
if ( fgets(filename, sizeof filename, stdin) != NULL )
{
char *newline = strchr(filename, '\n'); /* search for newline character */
if ( newline != NULL )
{
*newline = '\0'; /* overwrite trailing newline */
}
printf("filename = \"%s\"\n", filename);
}
filepointer=fopen(filename,"r");
fseek(filepointer, 0, SEEK_END); // seek to end of file
size = ftell(filepointer);
printf("Size=%d\n",size);
fseek(filepointer, 0, SEEK_SET);
int holderarray[size];
for(i=0; i<size; i++)
fscanf(filepointer, "%d", &holderarray[i]);
nrows=holderarray[0];
printf("Number of rows/columns=%d\n",nrows);
if (filepointer == NULL)
{
fprintf(stderr, "Can't open input file in.list!\n");
exit(1);
}
}
Everything works as expected up until this point. I can't visualize how to add the first half of the values to the new 2 dimensional arrays, hopefully you guys can help. Here's my brainstorming in codeblocks.
int matrix1[nrows][nrows];
int matrix2[nrows][nrows];
for (i=1; i<sizeof(holderarray);i++)
{
for (j=0;j<nrows;j++)
{
matrix[i][j]=holderarray[j];
}
for (i=0;i<sizeof(nrows);i++)
{
for (j=0;j<sizeof(nrows);j++)
{
printf("%d",matrix[i][j]);
}
}
return 0;
You can't dynamically declare arrays in standard C like this based on variables that aren't known at compile time:
int matrix1[nrows][nrows];
int matrix2[nrows][nrows];
What you need to do, if you aren't using C99 or a later version, is use the malloc function that dynamically allocates memory for you:
int **matrix1, **matrix2;
int i;
matrix1 = malloc(nrows * sizeof(int*));
matrix2 = malloc(nrows * sizeof(int*));
for(i = 0; i < nrows; i++) {
matrix1[i] = malloc(nrows * sizeof(int));
matrix2[i] = malloc(nrows * sizeof(int));
}
Two-dimensional arrays in C are treated as pointers to pointers. Each pointer is a reference to a contiguous chunk of memory that contains integers (i.e. the rows of your matrix), and then we use a pointer to a pointer as a reference to another contiguous chunk of memory that contains references/pointers to the first element in all of these rows.
This image (not mine) may help: DYNAMIC 2D ARRAY
Note that this dynamically allocated memory should be freed when you are finished using it:
for(int i = 0; i < nrows; i++) {
free(matrix1[i]);
free(matrix2[i]);
}
free(matrix1);
free(matrix2);
这篇关于创建从在C文件输入的二维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!