C 语言 fwrite 和 fread 文件读写操作示例 2个
2011-11-05 14:46
519 查看
#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); // 把浮点数组写到文件 myfile.bin
fclose(pFile); // 关闭文件
float read[3];
pFile = fopen("myfile.bin" , "rb"); // 重新打开文件读操作
fread(read , 1 , sizeof(read) , pFile); // 从文件中读数据
printf("%f\t%f\t%f\n", read[0], read[1], read[2]);
fclose(pFile); // 关闭文件
return 0;
} /* 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;
}
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-10/46310p2.htm
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-10/46310p2.htm本文出自 “prothes blog” 博客,请务必保留此出处http://prothesman.blog.51cto.com/8610862/1411694
int main()
{
FILE* pFile;
float buffer[] = { 2.0 , 3.0 , 8.0 };
pFile = fopen("myfile.bin" , "wb"); // 打开文件写操作
fwrite(buffer , 1 , sizeof(buffer) , pFile); // 把浮点数组写到文件 myfile.bin
fclose(pFile); // 关闭文件
float read[3];
pFile = fopen("myfile.bin" , "rb"); // 重新打开文件读操作
fread(read , 1 , sizeof(read) , pFile); // 从文件中读数据
printf("%f\t%f\t%f\n", read[0], read[1], read[2]);
fclose(pFile); // 关闭文件
return 0;
} /* 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;
}
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-10/46310p2.htm
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-10/46310p2.htm本文出自 “prothes blog” 博客,请务必保留此出处http://prothesman.blog.51cto.com/8610862/1411694
相关文章推荐
- C 语言 fwrite 和 fread 文件读写操作示例
- C 语言 fwrite 和 fread 文件读写操作示例
- [Linux流操作]使用fread和fwrite函数读写文件
- 文件读写操作之一 <二进制读写操作> fwrite与fread
- c语言文件读写示例(c语言文件操作)
- Linux下C语言的文本文件读写(fputc,fgetc,fwrite,fread对文件读写操作)
- C语言文件流操作的二进制读写和定位(fwrite、fread、fseek)
- C语言文件读写例题,fgetc,fputc,fgets,fputs,fread,fwrite,fscanf,fprintf
- C语言文件读写常识(fseek, ftell, fread, fwrite, fopen)
- fread()和fwrite()函数读写文件操作
- [VB.NET源码]文件读写与数据库操作示例
- C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
- C语言文件读写操作总结
- 文件读写结构体信息...fread, fwrite
- C/C++文件的操作(fread() fwrite())
- 编程基本功——C标准文件的读写操作示例
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- 15:文件操作(C标准库函数)fopen/fclose/fread/fwrite/fseek/fprintf/fscanf
- 示例1.7 对字符文件的读写操作
- C++ 文件读写函数之——fopen、fread和fwrite