简体   繁体   English

从文本文件中读取,存储数据C ++的最佳方法

[英]Reading from a text file, best way to store data C++

Basicly I have a text file which i need t read-in the values so the program can manipulate them. 基本上我有一个文本文件,我需要读入值,以便程序可以操作它们。

Im using C++ and i have written working code to tell if the file exists or not. 我使用C ++,我已经编写了工作代码来判断文件是否存在。

The text file is formatted like this: 文本文件的格式如下:

1    7
8    10
20   6
3    14
...

The values on the left are X values and the values on the right are Y values. 左边的值是X值,右边的值是Y值。 (The space in the middle is a tab) How do I extract this data? (中间的空格是一个选项卡)如何提取此数据? say to pass them into a class like this... 说把它们传递给像这样的班级......

myVector(X,Y);

Also, I guess before I can use it in a class I have to TryParse to change it from a string to int right? 另外,我想在我可以在类中使用它之前我必须将TryParse从字符串更改为int对吗? can C++ do this? C ++可以这样做吗?

Thank you! 谢谢!

I would be writing something like this if I were you. 如果我是你,我会写这样的东西。 Note, this is just prototype code, and it was not even tested. 注意,这只是原型代码,甚至没有经过测试。

The fundamental idea is to read twice in a line, but with different delimiters. 基本思想是在一行中读取两次,但使用不同的分隔符。 You would read with the tab delimiter first, and then just the default line end. 您将首先使用制表符分隔符进行读取,然后仅使用默认行结束。

You need to make sure to gracefully quit the loop when you do not have anything more to read, hence the breaks, albeit the second could be enough if your file is "correct". 当你没有更多要阅读的内容时,你需要确保优雅地退出循环,因此如果你的文件是“正确的”,那么第二个就足够了。

You will also need to make sure to convert to the proper type that your vector class expects. 您还需要确保转换为矢量类所需的正确类型。 I assumed here that is int, but if it is string, you do not need the conversion I have put in place. 我假设这是int,但如果它是字符串,则不需要我已经实现的转换。

#include <string>
#include <fstream>

using namespace std;

void yourFunction()
{
    ..
    ifstream myfile("myfile.txt");
    string xword, yword;
    while (1) {
        if (!getline(myfile, xword, '\t'))
            break;
        if (!getline(myfile, yword))
            break;
        myVector.push_back(stoi(xword), stoi(yword));
    }
    ...
}

This sort of parsing could be done in one line with boost.spirit: 这种解析可以使用boost.spirit在一行中完成:

qi::phrase_parse(begin, end, *(qi::int_ > qi::int_ > qi::eol), qi::ascii::blank, v);

The grammar could be read as: "read one int, then one int, then one EOL (end of line) (\\n or \\r\\n, depends on locale), as many time as possible". 语法可以读作:“读取一个int,然后一个int,然后一个EOL(行尾)(\\ n或\\ r \\ n,取决于语言环境),尽可能多的时间”。 Between ints and EOL can be found blank characters (eg spaces or tabs). 在int和EOL之间可以找到空白字符(例如空格或制表符)。

Advantages: rather than std::getline loops, code is more clear/concise. 优点:而不是std :: getline循环,代码更清晰/简洁。 spirit.qi get you more powerful control and you don't need stoi calls. spirit.qi让你更强大的控制,你不需要stoi电话。 Drawbacks: build-depends (no depends) to spirit.qi, compilation time. 缺点:构建 - 依赖(不依赖)spirit.qi,编译时间。

#include <iostream>
#include <fstream>
#include <vector>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace spirit = boost::spirit;
namespace qi = spirit::qi;

int main(int argc, char **argv)
{
  std::ifstream in(argv[1], std::ios_base::in);

  std::string storage;
  in.unsetf(std::ios::skipws);
  spirit::istream_iterator begin(in), end;

  std::vector<std::pair<int, int> > v;
  qi::phrase_parse(begin, end, *(qi::int_ > qi::int_ > qi::eol), qi::ascii::blank, v);

  for(const auto& p : v)
    std::cout << p.first << "," << p.second << std::endl;

  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM