c语言 怎么把float型的数写到文件中,这个涉及到文件操作。谢谢

qiaozheng2011 2011-09-29 10:01:36
c语言 怎么把float型的数写到文件中,这个涉及到文件操作。谢谢
...全文
343 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongwenjun 2011-09-29
  • 打赏
  • 举报
回复
/* fread example: read a complete file 读取一个完整的文件 */
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE* pFile; //文件指针
long lSize; // 用于文件长度
char* buffer; // 文件缓冲区指针
size_t result; // 返回值是读取的内容数量

pFile = fopen("myfile.bin" , "rb");
if (pFile == NULL) {fputs("File error", stderr); exit(1);} // 如果文件错误,退出1

// obtain file size: 获得文件大小
fseek(pFile , 0 , SEEK_END); // 指针移到文件末位
lSize = ftell(pFile); // 获得文件长度
rewind(pFile); // 函数rewind()把文件指针移到由stream(流)指定的开始处, 同时清除和流相关的错误和EOF标记

// allocate memory to contain the whole file: 为整个文件分配内存缓冲区
buffer = (char*) malloc(sizeof(char) * lSize); // 分配缓冲区,按前面的 lSize
if (buffer == NULL) {fputs("Memory error", stderr); exit(2);} // 内存分配错误,退出2

// copy the file into the buffer: 该文件复制到缓冲区
result = fread(buffer, 1, lSize, pFile); // 返回值是读取的内容数量
if (result != lSize) {fputs("Reading error", stderr); exit(3);} // 返回值如果不和文件大小,读错误

/* the whole file is now loaded in the memory buffer. */ //现在整个文件载入内存缓冲区

// 读到内存,看自己怎么使用了...............
// ...........


// terminate // 文件终止错误
fclose(pFile);
free(buffer);
return 0;
}

读了一下,这个代码范例,还是写的比较工整的,虽然有点啰嗦
我的注解只是自己啰嗦,当作学习时的笔记,实际写这么多注解,也不好读代码的
hongwenjun 2011-09-29
  • 打赏
  • 举报
回复
http://www.cplusplus.com/reference/clibrary/cstdio
点进去自己看 例子
/* fread example: read a complete file */
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE* pFile;
long lSize;
char* buffer;
size_t result;

pFile = fopen("myfile.bin" , "rb");
if (pFile == NULL) {fputs("File error", stderr); exit(1);}

// obtain file size:
fseek(pFile , 0 , SEEK_END);
lSize = ftell(pFile);
rewind(pFile);

// allocate memory to contain the whole file:
buffer = (char*) malloc(sizeof(char) * lSize);
if (buffer == NULL) {fputs("Memory error", stderr); exit(2);}

// copy the file into the buffer:
result = fread(buffer, 1, lSize, pFile);
if (result != lSize) {fputs("Reading error", stderr); exit(3);}

/* the whole file is now loaded in the memory buffer. */

// terminate
fclose(pFile);
free(buffer);
return 0;
}

hongwenjun 2011-09-29
  • 打赏
  • 举报
回复
#include <stdio.h>

int main()
{
FILE* pFile;
float buffer[] = { 2.0 , 3.0 , 8.0 };
pFile = fopen("myfile.bin" , "wb");
fwrite(buffer , 1 , sizeof(buffer) , pFile);
fclose(pFile);

float read[3];
pFile = fopen("myfile.bin" , "rb");
fread(read , 1 , sizeof(buffer) , pFile);
printf("%f\t%f\t%f\n", read[0], read[2], read[3]);
return 0;
}


写文件,读文件
luciferisnotsatan 2011-09-29
  • 打赏
  • 举报
回复
给机器看的二进制格式,直接写就是了。
如果要是给人看的文本格式,用fprintf
shi3590 2011-09-29
  • 打赏
  • 举报
回复
先用sprintf整合成字符串一起写?
hongwenjun 2011-09-29
  • 打赏
  • 举报
回复
/* fwrite example : write buffer */
#include <stdio.h>

int main ()
{
FILE * pFile;
char buffer[] = { 'x' , 'y' , 'z' };
pFile = fopen ( "myfile.bin" , "wb" );
fwrite (buffer , 1 , sizeof(buffer) , pFile );
fclose (pFile);
return 0;
}


自己改一下吧

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧