读取数值型数据

一维

input.txt 文件示例:

6

1 2 3 1 2 3

6 表示有 6 个数据

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

#define err_exit(x) do { printf("%s\n", x); exit(1); } while (0)
#define err_ifret(cond) do { if (cond) return ERROR; } while (0)

#define ERROR       -1
#define SUCCESS     0
#define MAX_SIZE    1000000

/* pint */
void malloc_pint(int **p, int size)
{
    *p = (int *)malloc(sizeof(int) * size);
}

int read_pint(FILE *fp, int *p, int size)
{
    for (int i = 0; i < size; i++) {
        err_ifret(fscanf(fp, "%d", &p[i]) == EOF);
    }
    return SUCCESS;
}

void clear_pint(int *p)
{
    if (p != NULL) {
        free(p);
    }
}

int fscanf_pint(FILE *fp, int *in)
{
    err_ifret(fscanf(fp, "%d", in) == EOF);
    err_ifret(*in < 0 || *in > MAX_SIZE);
    return SUCCESS;
}

int malloc_read(int **p, int size, FILE *fp)
{
    malloc_pint(p, size);
    err_ifret(read_pint(fp, *p, size) == ERROR);
    return SUCCESS;
}
/* pint */

struct matrix {
    int size;
    int *data;
};

/*
 * 读取文件失败返回 ERROR, 否则返回 SUCCESS
 */
int init_matrix_from_file(struct matrix *matrix, char *path)
{
    matrix->data = NULL;

    FILE *fp;
    err_ifret((fp = fopen(path, "r")) == NULL);

    err_ifret(fscanf_pint(fp, &matrix->size) == ERROR);

    err_ifret(malloc_read(&matrix->data, matrix->size, fp) == ERROR);

    fclose(fp);
    return SUCCESS;
}

void clear_matrix(struct matrix *matrix)
{
    clear_pint(matrix->data);
}

void print_matrix(struct matrix *matrix)
{
    for (int i = 0; i < matrix->size; i++) {
        printf("%d ", matrix->data[i]);
    }
    printf("\n");
}

int main(int argc, char *argv[])
{
    struct matrix matrix;
    if (init_matrix_from_file(&matrix, "input.txt") == ERROR) {
        err_exit("Read file error");
    }

    print_matrix(&matrix);

    clear_matrix(&matrix);
    return 0;
}

二维(更新不及时,请看一维)

input.txt 文件示例:

2 3

1 2 3
1 2 3

第一行的 2,3 表示下面的数据共有 2 行 3 列

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

#define err_exit(x) do { printf("%s\n", x); exit(1); } while (0)
#define err_ifret(cond) do { if (cond) return ERROR; } while (0)

#define ERROR       -1
#define SUCCESS     0
#define MAX_ROW     1000
#define MAX_COL     1000

/* ppint */
void malloc_ppint(int ***p, int row, int col)
{
    *p = (int **)malloc(sizeof(int *) * row);
    for (int i = 0; i < row; i++) {
        (*p)[i] = (int *)malloc(sizeof(int) * col);
    }
}

int read_ppint(FILE *fp, int **p, int row, int col)
{
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            err_ifret(fscanf(fp, "%d", &p[i][j]) == EOF);
        }
    }
    return SUCCESS;
}

void clear_ppint(int **p, int row)
{
    if (p != NULL) {
        for (int i = 0; i < row; i++) {
            free(p[i]);
        }
        free(p);
    }
}
/* pping */

struct matrix {
    int row;
    int col;
    int **data;
};

/*
 * 读取文件失败返回 ERROR, 否则返回 SUCCESS
 */
int init_matrix_from_file(struct matrix *matrix, char *path)
{
    matrix->data = NULL;

    FILE *fp;
    err_ifret((fp = fopen(path, "r")) == NULL);

    err_ifret(fscanf(fp, "%d", &matrix->row) == EOF);
    err_ifret(matrix->row < 0 || matrix->row > MAX_ROW);
    err_ifret(fscanf(fp, "%d", &matrix->col) == EOF);
    err_ifret(matrix->col < 0 || matrix->col > MAX_COL);

    malloc_ppint(&matrix->data, matrix->row, matrix->col);
    err_ifret(read_ppint(fp, matrix->data, matrix->row, matrix->col) == ERROR);

    fclose(fp);
    return SUCCESS;
}

void clear_matrix(struct matrix *matrix)
{
    clear_ppint(matrix->data, matrix->row);
}

void print_matrix(struct matrix *matrix)
{
    for (int i = 0; i < matrix->row; i++) {
        for (int j = 0; j < matrix->col; j++) {
            printf("%d ", matrix->data[i][j]);
        }
        printf("\n");
    }
}

int main(int argc, char *argv[])
{
    struct matrix matrix;
    if (init_matrix_from_file(&matrix, "input.txt") == ERROR) {
        err_exit("Read file error");
    }

    print_matrix(&matrix);

    clear_matrix(&matrix);
    return 0;
}

一维模拟二维(更新不及时,请看一维)

input.txt 文件示例:

2 3

1 2 3
1 2 3

第一行的 2,3 表示下面的数据共有 2 行 3 列

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

#define err_exit(x) do { printf("%s\n", x); exit(1); } while (0)
#define err_ifret(cond) do { if (cond) return ERROR; } while (0)

#define ERROR       -1
#define SUCCESS     0
#define MAX_ROW     1000
#define MAX_COL     1000

/* pint */
void malloc_pint(int **p, int row, int col)
{
    *p = (int *)malloc(sizeof(int) * row * col);
}

int read_pint(FILE *fp, int *p, int row, int col)
{
    for (int i = 0; i < row * col; i++) {
        err_ifret(fscanf(fp, "%d", &p[i]) == EOF);
    }
    return SUCCESS;
}

void clear_pint(int *p)
{
    if (p != NULL) {
        free(p);
    }
}
/* pint */

struct matrix {
    int row;
    int col;
    int *data;
};

/*
 * 读取文件失败返回 ERROR, 否则返回 SUCCESS
 */
int init_matrix_from_file(struct matrix *matrix, char *path)
{
    matrix->data = NULL;

    FILE *fp;
    err_ifret((fp = fopen(path, "r")) == NULL);

    err_ifret(fscanf(fp, "%d", &matrix->row) == EOF);
    err_ifret(matrix->row < 0 || matrix->row > MAX_ROW);
    err_ifret(fscanf(fp, "%d", &matrix->col) == EOF);
    err_ifret(matrix->col < 0 || matrix->col > MAX_COL);

    malloc_pint(&matrix->data, matrix->row, matrix->col);
    err_ifret(read_pint(fp, matrix->data, matrix->row, matrix->col) == ERROR);

    fclose(fp);
    return SUCCESS;
}

void clear_matrix(struct matrix *matrix)
{
    clear_pint(matrix->data);
}

void print_matrix(struct matrix *matrix)
{
    for (int i = 0; i < matrix->row; i++) {
        for (int j = 0; j < matrix->col; j++) {
            printf("%d ", matrix->data[i * matrix->col + j]);
        }
        printf("\n");
    }
}

int main(int argc, char *argv[])
{
    struct matrix matrix;
    if (init_matrix_from_file(&matrix, "input.txt") == ERROR) {
        err_exit("Read file error");
    }

    print_matrix(&matrix);

    clear_matrix(&matrix);
    return 0;
}