• 大小: 2.63MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-08-10
  • 语言: C/C++
  • 标签: C语言  

资源简介

作为一个新手学习C语言读写数据,我整合了一下自己查找的一些资料,用C语言实现将一个文件读入内存中,里面有运行工具,和一个word文档代码。

资源截图

代码片段和文件信息

#include
#include
int getData(float **data_bufferint *data_size) {
//*data_buffer中存的是指针data_buffer的地址;
FILE *fp;
int error=0;
fp = fopen(“test.bin“ “rb“);
if (fp == NULL) {
printf(“Open data file failed!\n“);
return -1;
}
if (*data_size == 0) {
fseek(fp 0 SEEK_END);
*data_size = ftell(fp);
fclose(fp);
return 0;
}
*data_buffer = (float *)malloc(*data_size);
fseek(fp 0 SEEK_SET);
error = fread(*data_buffer sizeof(float) *data_size / sizeof(float) fp);
if (error != *data_size / sizeof(float)) {
printf(“Read data failed!\n“);
return -1;
}
fclose(fp);
return 0;
}

int check(float *data_test) {
FILE *fp;
float *data_buffer;
int data_size;
int error = 0;
fp = fopen(“test.bin“ “rb“);
if (fp == NULL) {
printf(“Open data file failed!\n“);
return -1;
}
fseek(fp 0 SEEK_END);
data_size = ftell(fp);
data_buffer = (float *)malloc(data_size);
fseek(fp 0 SEEK_SET);
error = fread(data_buffer sizeof(float) data_size / sizeof(float) fp);
if (error != data_size / sizeof(float)) {
printf(“Read data failed!\n“);
return -1;
}

for (int i = 0; i < data_size / sizeof(float); i++) {
if (data_test[i] - data_buffer[i] == 0) {

}
else {
return -2;
}
}
fclose(fp);
return 0;
}

int main() {
float *data_buffer=NULL;
int data_size = 0;
char error = 0;
error = getData(&data_buffer &data_size);
if (error!=0) {
printf(“Get data_size failed!\n“);
return -1;
}
error = getData(&data_buffer &data_size);
if (error != 0) {
printf(“Read data failed!\n“);
return -1;
}
error = check(data_buffer);
if (error == -2) {
printf(“Read failed!\n“);
}
else
printf(“successed!\n“);
free(data_buffer);
return 0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-11 15:54  c读写数据\
     目录           0  2016-09-28 12:34  c读写数据\.vs\
     目录           0  2016-09-28 12:34  c读写数据\.vs\readWriteByLine\
     目录           0  2016-09-28 12:34  c读写数据\.vs\readWriteByLine\v14\
     文件       33792  2016-09-29 20:44  c读写数据\.vs\readWriteByLine\v14\.suo
     目录           0  2016-09-29 21:41  c读写数据\Debug\
     文件       38400  2016-09-29 21:43  c读写数据\Debug\readWriteByLine.exe
     文件      308908  2016-09-29 21:43  c读写数据\Debug\readWriteByLine.ilk
     文件      618496  2016-09-29 21:43  c读写数据\Debug\readWriteByLine.pdb
     目录           0  2018-12-11 14:35  c读写数据\readWriteByLine\
     文件     1900544  2018-12-11 15:54  c读写数据\readWriteByLine.sdf
     文件        1327  2016-09-28 12:34  c读写数据\readWriteByLine.sln
     文件       20480  2018-12-11 15:54  c读写数据\readWriteByLine.v12.suo
     文件     1515520  2016-09-29 10:45  c读写数据\readWriteByLine.VC.db
     文件          32  2016-09-29 19:22  c读写数据\readWriteByLine.VC.VC.opendb
     目录           0  2016-09-29 20:51  c读写数据\readWriteByLine\Debug\
     文件       11676  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\main.obj
     文件         967  2016-09-29 20:51  c读写数据\readWriteByLine\Debug\readWriteByLine.Build.CppClean.log
     文件         326  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.log
     目录           0  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.tlog\
     文件         638  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.tlog\CL.command.1.tlog
     文件        2138  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.tlog\CL.read.1.tlog
     文件         528  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.tlog\CL.write.1.tlog
     文件        1182  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.tlog\link.command.1.tlog
     文件        2748  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.tlog\link.read.1.tlog
     文件         510  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.tlog\link.write.1.tlog
     文件         201  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\readWriteByLine.tlog\readWriteByLine.lastbuildstate
     文件       35840  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\vc140.idb
     文件       77824  2016-09-29 21:43  c读写数据\readWriteByLine\Debug\vc140.pdb
     文件        1810  2016-09-29 21:43  c读写数据\readWriteByLine\main.c
     文件        5819  2016-09-28 20:31  c读写数据\readWriteByLine\readWriteByLine.vcxproj
............此处省略7个文件信息

评论

共有 条评论