zoukankan      html  css  js  c++  java
  • linux c读取分割符文件的每一行最后一个字符串

    假如分割符文件为

    11 22 33

    44 55 232 66

    只获取33,66数据,代码如下,核心利用fgets读取到回车终止

    #include <sys/types.h>
    #include <stdio.h>
    #include <stdbool.h>
    #define MAX_LEN 1024
    bool ReadFile(char* filePath);
    int main(int argc, char** argv)
    {
        char* src = "//root//projects//ConsoleApplication3//src.txt";
        ReadFile(src);
        getchar();
    	return 0;
    }
    bool ReadFile(char* filePath)
    {
    
        char src[MAX_LEN];
        char dst[MAX_LEN];
        FILE* fp = fopen(filePath, "r");
        if (!fp)
        {
            printf("can't open file
    ");
            return false;
        }
        while (!feof(fp)) {
            memset(src, 0x00, MAX_LEN);
            fgets(src, MAX_LEN, fp );
            int lineLength = strlen(src);
            int iLastPosSpace=0;
            for (int j = lineLength - 1; j >= 0; j--)
            {
                if (src[j] == ' ')
                {
                    memset(dst, 0x00, MAX_LEN);
                    iLastPosSpace = j;
                    memcpy(dst, src+iLastPosSpace+1, lineLength-iLastPosSpace+1);
                    printf(dst);
                    break;
                }
            }
        }
        fclose(fp);
        return true;
    }
    

      

      

    1、建了一个小群:616945527(软件), 欢迎大家加入,加群口令abc123,硬件嵌入式开发者推荐75764412(单片机)。
    闲置域名www.nsxz.com出售(等宽等高字符四字域名,可组合多种有意义词语)。
  • 相关阅读:
    最大熵原理
    python单引号、双引号和三双引号的区别
    python的字典
    hadoop jar xxxx.jar 执行的流程
    java 正则表达式
    secureCRT中vim个性化设置
    python关系运算符的全称
    C# 分割字符
    委托(delegate)
    在C#中,委托(delegate)
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/14537135.html
Copyright © 2011-2022 走看看