簡體   English   中英

從文件中讀取二維數組並打印

[英]Reading two dimensional array from file and printing it

我的代碼有一些問題。 一切安好; gcc 沒有顯示錯誤或警告; valgrind 說沒有內存泄漏,但我的數組沒有打印出來,我不知道為什么。 fscanf()工作不正常還是我打印不正確?

#include <stdio.h>
#include <stdlib.h>

int arrsize (FILE *file)
{
    if (file == NULL) {
        printf("Error: file pointer is null.");
        return 0;
    }
    int sizer;
    fscanf(file, "%d", &size);
    return size;
}


int main()
{
    int i = 0, j = 0, k = 0,a=0;

    FILE *fp;
    if ((fp = fopen("matrix.txt", "r")) == NULL)
    {
        printf("Error: file pointer is null.\n");
        return 1;
    }
    int size = arrsize(fp);

    printf("Array size is %d x %d \n", size, size);

    double **array = (double **)malloc(size * sizeof(double *));
    for (i; i < size; i++)
       array[i] = (double *)malloc(size * sizeof(double));

    for (i; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            fscanf(fp, " %lf", &array[i][j]); 
            printf(" %lf ",array[i][j]);
        }
    }

    for (k ; k < size; k++) {
        free(array[k]);        
    }
    free(array);

    fclose(fp);
    return 0;
}

檢查fscanfmalloc的返回以確保成功。
添加fflush ( stdout); 到打印循環,因為格式字符串沒有換行符。

#include <stdio.h>
#include <stdlib.h>

int arrsize (FILE *file)
{
    if (file == NULL) {
        printf("Error: file pointer is null.");
        return 0;
    }
    int size;
    if ( 1 == fscanf(file, "%d", &size)) {
        return size;
    }
    return -1;
}


int main()
{
    int i = 0, j = 0, k = 0;

    FILE *fp;
    if ((fp = fopen("matrix.txt", "r")) == NULL)
    {
        printf("Error: file pointer is null.\n");
        return 1;
    }
    int size = arrsize(fp);
    if ( -1 == size) {
        fprintf( stderr, "Error: problem parsing array size.\n");
        return 1;
    }

    printf("Array size is %d x %d \n", size, size);

    double **array = NULL;
    if ( NULL != ( array = malloc(size * sizeof(double *)))) {
        for (i = 0; i < size; i++) {
            if ( NULL == ( array[i] = malloc(size * sizeof(double)))) {
                fprintf ( stderr, "problem allocation array[]\n");
                while ( i) {
                    i--;
                    free ( array[i]);
                }
                free ( array);
                return 1;
            }
        }
    }
    else {
        fprintf ( stderr, "problem allocation array\n");
        return 1;
    }

    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            if ( 1 == fscanf(fp, "%lf", &array[i][j])) {
                printf(" %lf ",array[i][j]);
            }
            else {
                fprintf ( stderr, "problem parsing double\n");
                break;
            }
        }
        fflush ( stdout);
    }

    for (k = 0; k < size; k++) {
        free(array[k]);
    }
    free(array);

    fclose(fp);
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM