C/C++以行的方式读取文件

C语言以行的方式读取文件,可以使用fgets函数,例子如下:


int ReadFileByLine()
{
    std::string strPath = "C:\\StoragePath\\15A0ACF042B76706BD\\00\\20191217\\Update.txt";
    FILE *pFp = fopen(strPath.c_str(), "ab+");
    int nWriteCnt = 0;
    char szWrite[512] = "File Index=12323 Name=123.avi Path=F:/qq/745357068/FileRecv Size=122252 FileType=1 PUID=155282621597026847 \r\n";
    while(nWriteCnt < 100000)
    {
        fwrite(szWrite, sizeof(char), strlen(szWrite), pFp);
        nWriteCnt++;
    }
    fclose(pFp);

    DWORD nTime = GetTickCount();
    nWriteCnt = 0;
    char szBuff[1024] = {0};
    pFp = fopen(strPath.c_str(), "ab+");
    while(!feof(pFp))
    {
        nWriteCnt++;
        memset(szBuff, 0, sizeof(szBuff));
        fgets(szBuff, 1024, pFp);
        if (nWriteCnt %100 == 1)
        {
            printf("Read File Line = %d\n", nWriteCnt);
        }

        if (strlen(szBuff) == 0)
        {
            continue;
        }

        char *pBegin = strstr(szBuff, "Index") + 6; // strlen("Index=");
        char *pEnd = strstr(pBegin, " ");
        std::string strIndex(pBegin, pEnd-pBegin);

        pBegin = strstr(szBuff, "Name") + 5; //  strlen("Name=");
        pEnd = strstr(pBegin, " ");
        std::string strName(pBegin, pEnd - pBegin);

        pBegin = strstr(szBuff, "Path") + 5 ; //strlen("Path=");
        pEnd = strstr(pBegin, " ");
        std::string strPath(pBegin, pEnd - pBegin);

        pBegin = strstr(szBuff, "Size") + 5; // strlen("Size=");
        pEnd = strstr(pBegin, " ");
        std::string strSize(pBegin, pEnd - pBegin);

        pBegin = strstr(szBuff, "FileType") + 9; // strlen("FileType=");
        pEnd = strstr(pBegin, " ");
        std::string strFileType(pBegin, pEnd - pBegin);

        pBegin = strstr(szBuff, "PUID") +  5; // strlen("PUID=");
        pEnd = strstr(pBegin, " ");
        std::string strPUID(pBegin, pEnd - pBegin);

    }
}

先按照一定的结构,写入数据,然后再按照行的方式读取出数据并解析相关数据

 

C++除了兼容了fgets函数,还可以通过getline()函数,以流的方式一行一行的读取文件

int ReadFileByLine(const char *pFile)
{
    std::string strLine;
    int nExist = access(pFile, F_OK);
    if(nExist == 0)
    {
        ifstream inFile(pFile);
        while(std::getline(inFile, strLine))
        {
            std::cout<< strLine.c_str();
        }

        inFile.close();
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C/C++编程中,判断INI文件中某个节中键值对是否全部取完毕可以通过以下步骤: 1. 打开INI文件:使用 fopen 函数打开INI文件,获取文件指针。 2. 定位到指定节:通过取每一行的内容,并判断该行是否是目标节的开始行(以'['开头)来定位到指定节。 3. 取键值对:在定位到目标节后,可以通过 fgets 函数逐行取节的内容,判断取的内容是否是键值对(包含'='字符),如果是,则进行处理。 4. 判断是否取完毕:判断条件可以有多种方式,例如可以在循环过程中使用计数器记录取的键值对数量,然后与目标节中的键值对总数进行对比,如果相等则表示取完毕。 5. 关闭INI文件:使用 fclose 函数关闭打开的INI文件。 以下是一个简单的示例代码: ```c #include <stdio.h> #include <string.h> int main() { FILE *file; char line[256]; int count = 0; int targetCount = 0; int isInTargetSection = 0; // 打开INI文件 file = fopen("config.ini", "r"); if (file == NULL) { printf("无法打开INI文件\n"); return -1; } // 定位到指定节 while (fgets(line, sizeof(line), file) != NULL) { if (line[0] == '[' && strstr(line, "[TargetSection]") != NULL) { isInTargetSection = 1; } if (isInTargetSection && strchr(line, '=') != NULL) { targetCount++; } // 判断是否取完毕 if (targetCount > 0 && count == targetCount) { break; // 取完毕 } } // 关闭INI文件 fclose(file); // 输出结果 printf("目标节中的键值对总数:%d\n", targetCount); printf("已取的键值对数量:%d\n", count); return 0; } ``` 注意:示例代码仅用于演示思路,请根据具体的需求和INI文件格式进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值