#include <stdio.h> #include <stdlib.h> #include <string.h> int file_length(char* fileName) { FILE* fp; int file_set_val,file_end_val; fp = fopen(fileName, "r"); if(fp == NULL) { printf("[%s][%s]read file fail ",__LINE__,__FUNCTION__); return 0; } fseek(fp, 0 , SEEK_SET); file_set_val = ftell(fp); fseek(fp, 0 , SEEK_END); file_end_val = ftell(fp); fclose(fp); return file_end_val-file_set_val; } int only_read_file(char* file_name_cp, char* read_content_cp, int read_len_i) { FILE* fp; fp = fopen(file_name_cp, "r"); if(fp == NULL) { printf("read file fail "); return -1; } fread(read_content_cp, read_len_i, 1, fp); fclose(fp); return 0; } int main(void) { char* file_content = NULL; char buff[100] ={0}; char* p = NULL; char* file_name = "123.txt"; char* match_str = "weigth:"; int length = 0; length = file_length(file_name); file_content = malloc(length); memset(file_content, 0, length); only_read_file(file_name, file_content, length); length = strlen(match_str); printf("length:%d ", length); p = strstr(file_content, match_str); if(p) { printf("p:%s ", p); sscanf(p+length, "%s", buff); } printf(" buff:%s ", buff); return 0; }