You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <stdlib.h>
// 确保你的Sample结构已经被定义
typedef struct Sample {
// ... 定义你的结构体成员 ...
} Sample;
// 全局变量,用于存储读取的数据行数
int testSize = 0;
Sample * getTestData(const char * filename){
FILE * file = fopen(filename, "r"); // 打开文件
if(file == NULL){
printf("%s Encountered an error while opening the file!\n\a", filename);
return NULL;
}
// 在内存中分配足够的空间来存储一个Sample结构并将指向该内存块的指针存储在result变量中
Sample * result = (Sample *)malloc(sizeof(Sample) * testSize);
if (result == NULL) {
printf("Memory allocation failed!\n");
fclose(file);
return NULL;
}
int count = 0; // 初始化计数器
while(fscanf(file, "%f %f", &result[count].feature1, &result[count].feature2) == 2){
count++; // 每次成功读取一行数据后递增count
}
fclose(file); // 关闭文件
printf("%s The file has been successfully read!\n", filename);
return result; // 返回result
}