[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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2021-11-22 使用 Visual Studio 创建 .NET 控制台应用程序