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\n");
        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;
}

  

  

posted @ 2021-03-15 13:46  zhaogaojian  阅读(615)  评论(0编辑  收藏  举报