Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66334
  • 博文数量: 19
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-13 16:29
文章分类

全部博文(19)

文章存档

2010年(9)

2009年(10)

我的朋友

分类: C/C++

2010-05-27 11:34:09

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


阅读(401) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~