#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>
int main(void)
{
FILE * pfstream;
int row = 0;
int col = 0;
int i = 0;
int j = 0;
int item;
int *narray = NULL;
if( (pfstream = fopen( "input.txt", "r" )) == NULL ){
printf( "The file 'input.txt' can not be opened\n" );
exit(-1);
}
else{
fscanf(pfstream,"%d",&row);
fscanf(pfstream,"%d",&col);
if (row > 0 && col > 0)
{
narray = malloc(sizeof(int)*row*col);
while (fscanf(pfstream,"%d",&item) != EOF)
{
narray[i++] = item;
//if (i == row*col)
// break;
}
}
//printf( "The file 'input.txt' was opened\n" );
}
for(i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%4d",narray[i*col + j]);
}
printf("\n");
}
printf("\n");
fclose(pfstream);
free(narray);
return 0;
}
|