c读写文件与文件参数管理
来源:互联网 发布:linux set pythonpath 编辑:程序博客网 时间:2024/06/14 03:08
直接上代码,代码简单易懂。
#include<stdio.h>#define PARAM_CONFIG "/home/lkg/test/config"typedef struct{ unsigned short int id; int offset;}param_offset;typedef struct { char c; int h; unsigned short n; char s[4];}st; ram_offset id_offset[3] = {▽ {1,0}, {2,8}, {3,108}, }; int param_write_cfg(st* buffer, unsigned short int param_id){ int ret, i, offset; FILE *fp; i = 0; while(id_offset && id_offset[i].id){ if(param_id == id_offset[i].id){ offset = id_offset[i].offset; break; } i++; } printf("id = %d, offset = %d\n", param_id, offset); if((fp = fopen(PARAM_CONFIG, "rb+")) == NULL) { if((fp = fopen(PARAM_CONFIG, "wb+")) == NULL) { printf("Can't open %s, program will to exit.\n", PARAM_CONFIG); return -1; } } fseek(fp, offset, SEEK_SET); fwrite(buffer, sizeof(*buffer), 1, fp); printf("st c = %d, h = %d n =%u s = %s \n",buffer->c, buffer->h,buffer->n,buffer->s); fclose(fp); return 0;}int param_read_cfg(st* buffer, unsigned short int param_id){ int ret, i, offset; FILE *fp; ~/test/param.c[+1] [c] unix utf-8 1:17/81 "param.c" 81L, 2187C writtenlkg@ubuntu:~/test$ gcc param.c lkg@ubuntu:~/test$ ./a.out id = 2, offset = 8st c = 5, h = 7 n =0 s = didi id = 3, offset = 108st c = 6, h = 2 n =0 s = dada id = 2, offset = 8st c = 5, h = 7 n =0 s = didi id = 3, offset = 108st c = 6, h = 2 n =0 s = dada lkg@ubuntu:~/test$ vi param.c * *读取文件指定id位置存储的结构体内容 * */int param_read_cfg(st* buffer, unsigned short int param_id){ int ret, i, offset; FILE *fp; param_offset id_offset[3] = { {1,0}, {2,8}, {3,108}, }; i = 0; while(id_offset && id_offset[i].id){ if(param_id == id_offset[i].id){ offset = id_offset[i].offset; break; } i++; } printf("id = %d, offset = %d\n", param_id, offset); if((fp = fopen(PARAM_CONFIG, "rb+")) == NULL) { printf("Can't open %s, program will to exit.", PARAM_CONFIG); return -1; } fseek(fp, offset, SEEK_SET); fread(buffer, sizeof(*buffer), 1, fp); printf("st c = %d, h = %d n =%u s = %s \n",buffer->c, buffer->h,buffer->n,buffer->s); fclose(fp); return 0; }int main(){// char version[] = "1.0.5"; st t; st tt = {5,7,0,"didi"}; st tt1 = {6,2,0,"dada"};// param_write_cfg(version,1); param_write_cfg(&tt,2); param_write_cfg(&tt1,3); param_read_cfg(&t,2); param_read_cfg(&t,3); return 0;}
阅读全文
0 0
- c读写文件与文件参数管理
- csv文件读写参数
- 在c语言之中文件的读写和函数与函数之间参数的调用
- 【C/C++】文件读写
- C的文件打开关闭与读写
- Linux与C语言的文件读写
- C语言读写文件与二进制文件
- C:文件操作-读写字符与读写行(!!!好)
- c语言读写文件
- 文件读写(c)
- C#-读写INI文件
- [C++]文件读写操作
- C#读写INI文件
- C 读写文件
- C读写ini文件
- c#FileStream文件读写
- 读写文件 c
- WinAPI C读写文件?
- action 和servlet关系
- relu和crelu使用
- iOS之sqlite和FMDB
- 欢迎使用CSDN-markdown编辑器
- Angular Ionic概述
- c读写文件与文件参数管理
- 正则 正整数bug
- eclipse转android studio解决GBK编码转换为UTF-8格式的方法
- 百度地图api web获取当前位置浏览器JavaScript定位javaweb jquery
- 原生js上传图片功能 angular语法(可改写为其他语言版本)。
- 纯js实现俄罗斯方块详解与源码
- Error: com.android.ide.common.process.ProcessException
- 变量和数据类型-你可能需要加强的基础
- LintCode_167_链表求和