window下 对特定文件夹下文件操作
#include "stdafx.h" #include <direct.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <io.h> #define file_dir "E:\\ceshi\\data_test" #define deal_file_dir "E:\\ceshi\\resuart_file" #define Frame_header_count 96 //导出帧头 和 数据 可以放在同一个文件里面,也可以放在不同的文件里面 bool transformation_file(FILE * fd, long int datalenth,const char * name) { FILE * fd2; char * value_buffer; long int resurt = 0; char file_path[100] = { 0 }; value_buffer = (char *)malloc(datalenth); if (NULL == value_buffer) { printf("开辟空间失败"); goto err_malloc_faile; } resurt = fseek(fd, datalenth - Frame_header_count, SEEK_SET); if (resurt) { printf("fseek1失败"); goto err_fseek; } resurt = fread(value_buffer, 1, Frame_header_count, fd); if (resurt != Frame_header_count) { printf("1读取个数有误"); goto err_fseek; } resurt = fseek(fd, 0, SEEK_SET); if (resurt) { printf("fseek2失败"); goto err_fseek; } resurt = fread(value_buffer + Frame_header_count, 1, datalenth - Frame_header_count, fd); if (resurt != datalenth - Frame_header_count) { printf("2读取个数有误"); goto err_fseek; } //所有的数据都存储在value_buffer里面 //建立新的文件 存起来 strcpy(file_path, deal_file_dir); file_path[strlen(file_path)] = '\\'; strcat(file_path, name); fd2 = fopen(file_path, "wb"); if (fd2 == NULL) { printf("文件打开失败\n"); goto err_fseek; } resurt = fwrite(value_buffer, 1, datalenth, fd2); if (resurt != datalenth) { printf("文件写入失败\n"); goto err_write; } free(value_buffer); fclose(fd2); return true; err_write: fclose(fd2); err_fseek: free(value_buffer); err_malloc_faile: return false; } //文件处理 打开原来的文件 找到对应位置 bool deal_file(const char * filepath,const char * name) { long int datalenth; FILE * fd ; bool result; fd = fopen(filepath, "rb"); if (fd == NULL) { printf("文件打开失败\n"); goto file_open_err; } fseek(fd,0, SEEK_END); datalenth = ftell(fd); result = transformation_file(fd, datalenth, name); if (!result) { printf("transformation_file失败\n"); goto transformation_file_err; } printf("文件大小是多少%ld\n", datalenth); fclose(fd); return true; transformation_file_err: fclose(fd); file_open_err: return false; } int main() { char filename[100] = {0}; long file; bool result; struct _finddata_t find; _chdir(file_dir); if ((file = _findfirst("*.*", &find)) == -1L) { printf("空白!\n"); exit(0); } if ((strcmp(find.name, ".") == 0 || strcmp(find.name, "..") == 0)) { } else { strcpy(filename, file_dir); filename[strlen(filename)] = '\\'; strcat(filename, find.name); printf("%s\n", filename); printf("%s\n", find.name); } while (_findnext(file, &find) == 0) { if ((strcmp(find.name, ".") == 0 || strcmp(find.name, "..") == 0)) { continue; } memset(filename, 0, sizeof(filename)); strcpy(filename, file_dir); filename[strlen(filename) ] = '\\'; strcat(filename, find.name); printf("%s\n", filename); result = deal_file(filename, find.name); if (!result) { printf("deal_file 失败"); } } _findclose(file); return 0; }