【问题标题】:C++ , reading file lines by numbersC++,按数字读取文件行
【发布时间】:2015-07-09 15:31:09
【问题描述】:

我想制作一个按数字读取文件的控制台项目。示例:按数字 1 2 查找,它仅在包含这些数字的文件夹中的控制台文本行中打印

   bibi                ceki     1     2
 hasesh              cekiii     1     3
   krki                 cko     1     2

在这种情况下,它只会打印出“bibi ceki”和“krki cko”。 在我的代码中有很多缺失的东西。我没有一个循环来检查是否有正确的数字,但这是我能做的最好的和我尝试过的:

#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main() {
    char str1[10], str2[10];
    int raz, ode;
    ifstream infile("file.txt");
    while (infile.good()) {
        fscanf(infile, "%s %s %d %d", str1, str2, &raz, &ode); //this thing cant be used lik this 
        while(raz==1 && ode==2) {
            string sLine;
            getline(infile, sLine);
            cout << sLine << endl;
        }
    }
    infile.close();
    return 0;
}

如您所见,使用 fscanf 的行不起作用,我不知道该怎么做。

如果有更好的方法,我需要一些帮助和建议,请尽可能具体,我是 c++/c 新手。

【问题讨论】:

    标签: c++ ifstream


    【解决方案1】:

    您正在混合 C fscanf 函数和 C++ ifstream。

    我建议使用 C++,在这种情况下,您可以像这样使用 operator>>:

    std::string str1, str2 ;
    ...
    //substitute the following line with the one below
    //fscanf(infile, "%s %s %d %d", str1, str2,&raz,&ode);
    infile >> str1 >> str2 >> raz >> ode ;
    

    【讨论】:

    • 谈到 C++ 时,很高兴看到实际使用的 C++。不错!
    • 是的,你是对的,我在开始使用 c++ 之前使用的是 C。它不再调用错误,但仍然不打印行。谢谢
    • 它打印任何东西吗?你能看到你用调试器得到了什么吗?或者只是添加一个 std::cout
    • @kuki 试过了,效果很好。 raz==1 &amp;&amp; ode==2 打印 bibi cekikrki cko。而raz == 1 &amp;&amp; ode == 3 打印hasesh cekiii。我将while(raz==1 &amp;&amp; ode==2) 更改为if(raz==1 &amp;&amp; ode==2) { cout &lt;&lt; str1 &lt;&lt; " " &lt;&lt; str2 &lt;&lt; endl; }
    • 成功了!! :D @raymelfrancisco,我如何将您的评论标记为答案?
    【解决方案2】:

    您可以使用std::getline 逐行读取,直到代码匹配,当匹配时,您可以将push_back 名称转换为std::vector。您还可以使用分隔符构建文件,例如'|',这样您就可以一直读到该字符作为名称,其余字符作为代码。示例文件是:

    bibi ceki|1 2
    hasesh cekiii|1 3
    krki cko|1 2
    

    以下是您如何实现这一目标的示例:

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        ifstream in_file("file.txt", ios::in);
        vector<string> vec;
        string names; // store names like: "bibi ceki"
        string codes; // store codes like: "1 2"
    
        while (getline(in_file, names, '|')) {
            getline(in_file, codes, '\n');
            if (codes == "1 2")
                vec.push_back(names);
        }
    
        for (unsigned int i = 0; i != vec.size(); ++i)
            cout << vec[i] << endl;
    
        in_file.close();
    }
    

    输出:

    bibi ceki
    krki cko
    

    【讨论】:

    • 我遇到了一些奇怪的错误。 错误:C++98 模式下不允许基于范围的“for”循环|。我认为这会起作用,我只需要修复这个错误。
    • @kuki,您可以使用普通的 for 循环,我编辑了答案以使用它。基于范围的 for 循环在使用 -std=c++11 编译时有效
    • 感谢您的努力,看来我找到了另一种适用于我的 txt 文件类型的解决方案。
    猜你喜欢
    • 2012-03-03
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2021-12-19
    相关资源
    最近更新 更多