zoukankan      html  css  js  c++  java
  • 读取文件信息,并通过sscanf从中获取所需数据

    #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;
    }
  • 相关阅读:
    AMap公交线路查询
    AMap行政区查询服务
    使用JavaScript的Join方法
    获取layer.open弹出层的返回值
    MVC项目中WebViewPage的实战应用
    ASP.NET管道
    Android接收系统广播
    python字符串转换成数字
    mysql缓存
    两个矩阵对应位置的数据相加,并返回一个矩阵
  • 原文地址:https://www.cnblogs.com/liuyang92/p/6649209.html
Copyright © 2011-2022 走看看