鱼C论坛

 找回密码
 立即注册
查看: 382|回复: 1

为什么进不去这个函数啊

[复制链接]
发表于 2024-10-24 09:02:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
while (getline(InputFile, line)) {
            cout<<"enter successful"<<endl;
        size_t commaPos = line.find(L',');
        if (commaPos != wstring::npos) {
            wstring keyword = Trim(line.substr(0, commaPos));
            wstring wFilePath = Trim(line.substr(commaPos + 1));
            string filePath(wFilePath.begin(), wFilePath.end());
                       
            // 替换路径中的反斜杠
            filePath = ReplaceBackslashWithSlash(filePath);
                       
            // 检查 CSV 中的关键词是否在用户关键词列表中
            if (find(UKeyWords.begin(), UKeyWords.end(), keyword) != UKeyWords.end()) {
                wcout << "找到匹配关键词: " << keyword << endl;
                               
                // 获取文件信息               
                int fileSizeBytes = GetSizeBytes(filePath);  // 以字节获取文件大小
                pair<string, time_t> timeInfo = GetTime(filePath);
                string fileTime = timeInfo.first;
                time_t rawTime = timeInfo.second;

                // 如果文件存在且可访问,保存文件信息
                if (fileSizeBytes != -1 && rawTime != -1) {
                    matchedFiles.emplace_back(filePath, fileSizeBytes, fileTime, rawTime);
                    wcout << "文件路径: " << wFilePath << ", 大小: " << fileSizeBytes << " B, 修改时间: " << StringToWString(fileTime) << endl;
                }
                else {
                    wcerr << "无法访问文件: " << wFilePath << endl;
                }
            }
        }
    }
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-10-24 09:08:48 | 显示全部楼层
看起来您在尝试使用`getline()`函数读取文件中的每一行。这段代码的问题可能在于以下几个方面:

1. **代码不完整**:您只展示了`cout`而没有完成它,应该是:`cout << line << std::endl;`。

2. **包含头文件**:确保包含了必要的头文件:
  
  1. #include <iostream>
  2.    #include <fstream>
  3.    #include <string>
复制代码


3. **文件打开检查**:确保文件被正确打开。例如:
  
  1. std::ifstream InputFile("filename.txt");
  2.    if (!InputFile.is_open()) {
  3.        std::cerr << "无法打开文件" << std::endl;
  4.        return;
  5.    }
复制代码


4. **命名空间**:如果没有`using namespace std;`,需要在`getline`、`cout`前加上`std::`。

整体代码应该像这样:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>

  4. int main() {
  5.     std::ifstream InputFile("filename.txt");
  6.     if (!InputFile.is_open()) {
  7.         std::cerr << "无法打开文件" << std::endl;
  8.         return 1;
  9.     }

  10.     std::string line;
  11.     while (std::getline(InputFile, line)) {
  12.         std::cout << line << std::endl;
  13.     }

  14.     InputFile.close();
  15.     return 0;
  16. }
复制代码


确保文件名和路径正确,且文件存在。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 00:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表