文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>从文件读入整数并动态设置数组内容

从文件读入整数并动态设置数组内容

时间:2010-05-27  来源:louisin

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


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载