【问题标题】:How can i pass a file as a parameter in C++?如何在 C++ 中将文件作为参数传递?
【发布时间】:2016-09-08 00:55:12
【问题描述】:

我想做的基本上是编写一个函数,该函数将参数作为文件名,如果有多个文件,它将只将文件名作为参数,并且应该使用函数重复执行此操作。我怎样才能做到这一点? 谢谢。

txt文件是这样的 例如sorular.txt:

//世界上最拥挤的国家是哪个?
//中国
//美国
//德国
//澳大利亚
//中国

int main (){

    string array [5];
    string line;
    string answer;
    static int trueCount = 0;
    static int falseCount = 0;
    ifstream file("/Users/User/QuizMaker/Quiz Maker V2/sorular.txt");

    if(file.is_open()){
        cout << "Questions are loading... Please wait.."<<endl<<" ."<<endl<<" ."<<endl<<" ."<<endl;
        while (!file.eof()) {
            for (int i = 0; i<6; i++) {
                getline(file,array[i]);
            }

            for (int a = 0; a<5; a++) {
                  cout << array[a] << endl;
            }
            cin >> answer;

            if(answer == "C" || answer == "c") {
                cout << true;
                trueCount++;
            }
            else falseCount++;
        }

        cout << "You answered "<<trueCount << " questions as true" << endl;
        cout << "You answered "<<falseCount << " questions as false" << endl;
        file.close();
    } else cout << " not ıoen";

    cin.get();

    return 0;
}

【问题讨论】:

  • 当然,您可以接受来自命令行的参数。 main(int argc, char** argv) 中的 argcargv 是通往这些论点的途径。
  • 请问如何写一个以std::string为参数的函数?我觉得没有什么可以帮你的。你应该只学习最基本的。
  • 非常感谢您的建议。我实际上也在学习它们。但是,在其中一个项目中,我需要做这种事情。你对这个问题的解决方案是什么?我真的错过了哪里?谢谢@specializt
  • @oguzdumanoglu:请忽略specializt 的建议。在 C++ 中,指针并不那么重要。对于数组,使用 std::array&lt;std::string, 5&gt; 而不是您现在拥有的数组类型 std::string[5]

标签: c++ file loops input ifstream


【解决方案1】:

首先让我说,array 不能容纳元素 012345。它只分配了 5 个元素。


这可能会稍微扩展您的视野,但我认为正确的解决方案是在这里上课。所以我要一起破解这个,如果你有兴趣,你可以研究一下我做了什么,如果你不明白,请随时在评论中提问。

class foo : public iterator<input_iterator_tag, string> {
    const initializer_list<const char*> _files;
    decltype(_files)::const_iterator _filesIt;
    ifstream _file;
    string _line;

    void get() {
        string array[6];
        auto i = begin(array);

        while (i != end(array) && getline(_file, *i)) {
            advance(i, 1);
        }

        if (i == end(array)) {
            _line = accumulate(cbegin(array), cend(array), string(), [](const auto& a, const auto& b) { return a.empty() ? b : a + '\n' + b; });
        } else if(++_filesIt != cend(_files)) {
            _file.close();
            _file.open(*_filesIt);
            get();
        }
    }
public:
    foo() : _filesIt(cend(_files)) {}
    foo(foo& rhs) : _files(rhs._files), _filesIt(next(cbegin(_files), distance(cbegin(rhs._files), rhs._filesIt))), _line(rhs._line) {
        if (_filesIt != cend(_files)) {
            _file.open(*_filesIt);
            _file.seekg(rhs._file.tellg());
        }
    }
    foo(const initializer_list<const char*>&& files) : _files(files), _filesIt(cbegin(_files)), _file(*_filesIt) { get(); }
    const string& operator*() const { return _line; }
    const foo& operator++() {
        get(); 
        return *this;
    }
    const foo operator++(int) {
        foo result;

        get();
        return result;
    }
    bool operator==(const foo& rhs) { return distance(_filesIt, cend(_files)) == distance(rhs._filesIt, cend(rhs._files)); }
    bool operator!=(const foo& rhs) { return distance(_filesIt, cend(_files)) != distance(rhs._filesIt, cend(rhs._files)); }
};

虽然这门课看起来让人不知所措,但它极大地简化了您要编译的其余部分。使用此类,您的其余代码将显示为:

auto true_count = 0;
auto false_count = 0;

for_each(foo({"/Users/User/QuizMaker/Quiz Maker V2/sorular.txt", "foo.txt", "bar.txt"}), foo(), [&](const auto& i) {
    string answer;

    cout << i << endl;
    cin >> answer;

    if(answer == "C" || answer == "c") {
        cout << 1;
        true_count++;
    } else {
        false_count++;
    }
});
cout << "You answered "<< trueCount << " questions as true" << endl << "You answered " << falseCount << " questions as false" << endl;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多