[good]c语言读取文件中的数据到结构体和数组
#include <stdio.h> #include <string.h> #include <stdlib.h> #define BUF_SIZE 100 #define MAX_SIZE 100 // #define ROWS(arr) (sizeof(arr) / sizeof((arr)[0])) // #define COLS(arr) (sizeof((arr)[0]) / sizeof((arr)[0][0])) // void getDimensions(int array[rows][cols], int *rows, int *cols) // { // *rows = sizeof(array) / sizeof(array[0]); // *cols = sizeof(array[0]) / sizeof(array[0][0]); // } void readDataSize(FILE *file, int *rows_size, const int cols_size) { int eles_count = 0; double num = 0; int n = 0; *rows_size = 0; while (fscanf(file, "%lf", &num) == 1) { eles_count++; if (eles_count % cols_size == 0) { (*rows_size)++; n++; } } if (eles_count % cols_size != 0) { n++; (*rows_size)++; } // printf("total element count is %d\n", eles_count); // printf("total rows is %d\n", *rows_size); } double **readDataToArray(FILE *fp, int rows_size, int cols_size) { int i, j; double num; int arr_size = rows_size * cols_size; // dynamic allocate memory double **arr = (int **)calloc(rows_size, sizeof(int *)); for (i = 0; i < rows_size; i++) { arr[i] = (int *)calloc(cols_size, sizeof(int)); } i = 0; while (fscanf(fp, "%lf", &num) == 1) { if (i < arr_size) { arr[i / cols_size][i % cols_size] = num; i++; } } // print data printf("the data from file is:\n"); for (i = 0; i < rows_size; i++) { for (j = 0; j < cols_size; j++) { printf("%lf ", arr[i][j]); } printf("\n"); } return arr; } // close file void closeFile(FILE *fp) { if (fclose(fp) == 0) { printf("close file success\n"); // perror("close file success\n"); } else { // printf("close file failed\n"); perror("close file failed\n"); } } int main2() { int rows_size, cols_size = 5; FILE *fp = NULL; char *sFileName = "D:/WolfCode/c-learn/data.dat"; fp = fopen(sFileName, "r"); // open file and read data to get the rows_size if (fp != NULL) { readDataSize(fp, &rows_size, cols_size); closeFile(fp); } else { // printf("open file failed\n"); perror("open file failed:"); return 0; } // open file again and read data to array double **arr; fp = fopen(sFileName, "r"); if (fp != NULL) { arr = readDataToArray(fp, rows_size, cols_size); closeFile(fp); } else { // printf("open file failed\n"); perror("open file failed:"); return 0; } // getDimensions(arr, &rows_size, &cols_size); // printf("array rows count is:%d\narray cols count is %d\n", rows_size, cols_size); printf("done"); } char *getFullPath(char *path_folder, char *file_name) { char *fullpath = malloc(strlen(path_folder) + strlen(file_name) + 1); if (fullpath == NULL) { fprintf(stderr, "Failed to allocate memory\n"); return 1; } strcpy(fullpath, path_folder); strcat(fullpath, file_name); printf("Full path: %s\n", fullpath); // free(fullpath); // Don't forget to free the memory! return fullpath; } int main() { // char buf[BUF_SIZE]; typedef struct _sPerson { int num; char name[MAX_SIZE]; char email[MAX_SIZE]; int phone; } sPerson; sPerson *ps = (sPerson *)(malloc(sizeof(sPerson))); char *path = "d:/wolfcode/c-learn/"; char *filename = "data_struct.dat"; char *fullpath = getFullPath(path, filename); FILE *fp = NULL; // char *sFileName = "D:/WolfCode/c-learn/data_struct.dat"; fp = fopen(fullpath, "r"); if (fp != NULL) { printf("open file success\n"); while (fscanf(fp, "%d %s %s %d", &(ps->num), ps->name, ps->email, &(ps->phone)) == 4) { printf("%d %s %s %d\n", ps->num, ps->name, ps->email, ps->phone); } } else { perror("open file failed\n"); return -1; } // close file free(ps); return 0; } // // free memory // for (i = 0; i < rows_size; i++) // { // free(arr[i]); // } // free(arr);