C++ 通过C+;分隔.txt文件中的注释和数据+;fstream

C++ 通过C+;分隔.txt文件中的注释和数据+;fstream,c++,fstream,C++,Fstream,我有一些具有以下模式的.txt文件: #Some comments here bull rocket 3 trailer laker -12 #More comments there Warriors Pacers 9 基本上,关于以a#开头有一些评论 其他行包含两个字符串,后跟一个int 我需要这两个字符串和一个int逐个处理 我必须忽略任何空行或以#开头的行 我正在考虑使用ifstream.get()读取第一个字符,然后 如果它是一条直线,则丢弃整条直线# 但是,当涉及到数据时,我被

我有一些具有以下模式的.txt文件:

#Some comments here

bull rocket 3
trailer laker -12

#More comments there

Warriors Pacers 9
基本上,关于以a#开头有一些评论 其他行包含两个字符串,后跟一个int

我需要这两个字符串和一个int逐个处理 我必须忽略任何空行或以#开头的行

我正在考虑使用ifstream.get()读取第一个字符,然后 如果它是一条直线,则丢弃整条直线#

但是,当涉及到数据时,我被卡住了。我怎么读字符 然后得到第一个字符串?i、 我找到一个“b”,然后我需要 “公牛”。我该怎么办

感谢使用“
while(std::getline(is,line)){
”一次读取一行(
std::istream is
)文件流(
std::string line

如果
空()
或以
开头
继续
。在执行此检查之前,您可能希望修剪任何前导空格

否则,解析该行(可能使用
std::istringstream iss(line);iss>>first>>second>>value;
)。StackOverflow上有很多关于如何执行该位的好例子。

使用“
while(std::getline(is,line)){
”读取文件流(
std::istream is
)一行(
std::string line
)一次

如果
空()
或以
开头
继续
。在执行此检查之前,您可能希望修剪任何前导空格

否则,请解析该行(可能使用
std::istringstream iss(行);iss>>第一个>>第二个>>值;
)。StackOverflow上有很多关于如何执行该位的很好示例。

示例代码:

    ifstream in("file.txt");
    if (!in.is_open())
        return false;

    string line;
    while (getline(in, line))
    {
        istringstream iss(line, istringstream::in);

        if (!line.length())
            continue;

        if (line[0] == '#') // Ignore the line starts with #
            continue;

       vector<string> words;

        string word;
        while (iss >> word)
        {
                words.push_back(word);
        }

        // now you have all words of current line
        // you can use them to parse your file
    }
ifstream-in(“file.txt”);
如果(!in.is_open())
返回false;
弦线;
while(getline(in,line))
{
istringstream iss(行,istringstream::in);
如果(!line.length())
继续;
if(第[0]行=“#”)//忽略以开头的行#
继续;
向量词;
字符串字;
while(iss>>word)
{
单词。推回(单词);
}
//现在你有了当前行的所有单词
//您可以使用它们来解析文件
}
这是一个示例代码,您应该跳过#之前的空格。例如,左修剪非常有用。

示例代码:

    ifstream in("file.txt");
    if (!in.is_open())
        return false;

    string line;
    while (getline(in, line))
    {
        istringstream iss(line, istringstream::in);

        if (!line.length())
            continue;

        if (line[0] == '#') // Ignore the line starts with #
            continue;

       vector<string> words;

        string word;
        while (iss >> word)
        {
                words.push_back(word);
        }

        // now you have all words of current line
        // you can use them to parse your file
    }
ifstream-in(“file.txt”);
如果(!in.is_open())
返回false;
弦线;
while(getline(in,line))
{
istringstream iss(行,istringstream::in);
如果(!line.length())
继续;
if(第[0]行=“#”)//忽略以开头的行#
继续;
向量词;
字符串字;
while(iss>>word)
{
单词。推回(单词);
}
//现在你有了当前行的所有单词
//您可以使用它们来解析文件
}

这是一个示例代码,您应该跳过#之前的空格。例如,左修剪很有用。

行中的某些内容:

#include <iostream>
#include <string>
#include <fstream>
int main(){
  std::ifstream input("input");
  while (input.good()) {
    char c = input.peek();
    if (c == '#' || c == '\n') {
      input.ignore(256, '\n');
      continue;
    }   
    std::string s1, s2; 
    int i;
    input >> s1 >> s2 >> i;
    if (input.good())
      std::cout << s1 << " - " << s2 << " - " << i << std::endl;
  }
  input.close();
  return 0;
}
#包括
#包括
#包括
int main(){
标准::ifstream输入(“输入”);
while(input.good()){
char c=input.peek();
如果(c='#'| | c=='\n'){
输入。忽略(256,“\n”);
继续;
}   
std::字符串s1,s2;
int i;
输入>>s1>>s2>>i;
if(input.good())

std::cout行中的某些内容:

#include <iostream>
#include <string>
#include <fstream>
int main(){
  std::ifstream input("input");
  while (input.good()) {
    char c = input.peek();
    if (c == '#' || c == '\n') {
      input.ignore(256, '\n');
      continue;
    }   
    std::string s1, s2; 
    int i;
    input >> s1 >> s2 >> i;
    if (input.good())
      std::cout << s1 << " - " << s2 << " - " << i << std::endl;
  }
  input.close();
  return 0;
}
#包括
#包括
#包括
int main(){
标准::ifstream输入(“输入”);
while(input.good()){
char c=input.peek();
如果(c='#'| | c=='\n'){
输入。忽略(256,“\n”);
继续;
}   
std::字符串s1,s2;
int i;
输入>>s1>>s2>>i;
if(input.good())

STD::CUT< P>可选的@ mm.的回答,你可以使用C++ 11的新的<>代码>代码>。但是,注意到目前不是所有的标准库都完全实现了这个,所以如果需要的话,你可能也想退回到。
#include <fstream>
#include <iostream>
#include <sstream>

// note: Most C++11 regex implementations are not up to scratch, offer
// Boost.regex as an alternative.
#ifdef USE_BOOST_REGEX
#include <boost/regex.hpp>
namespace std
{
  using ::boost::regex;
  using ::boost::regex_match;
  using ::boost::smatch;
}
#else
#include <regex>
#endif

#include <string>
#include <tuple>
#include <vector>

int main()
{
  // open input file
  std::ifstream in("file.txt");
  if (!in.is_open()) return 1;
  // ECMAScript syntax!
  std::regex empty_or_comment_re("\\s*(?:#.*)?");
  // note: only matches integers
  std::regex values_re("\\s*(\\S+)\\s+(\\S+)\\s+(-?\\d+)\\s*");
  // will contain the results
  std::vector<std::tuple<std::string, std::string, int> > results;
  size_t lineno = 0; // for error reporting
  std::string line;
  // read lines
  while (getline(in, line))
  {
    ++lineno;
    // match empty or comment lines
    if (regex_match(line, empty_or_comment_re)) continue;
    // match lines containing data
    std::smatch match;
    if (!regex_match(line, match, values_re))
    {
      std::cerr<< "ERROR: malformed line in file.txt, line " << lineno
        << ".\n";
      return 1;
    }
    // read integer from match
    int n;
    std::istringstream iss(match[3]);
    iss >> n;
    // append to results
    results.push_back(std::make_tuple(match[1], match[2], n));
  }
}
#包括
#包括
#包括
//注意:大多数C++11正则表达式的实现都不是最新的
//Boost.regex作为替代方案。
#ifdef使用_BOOST_REGEX
#包括
名称空间标准
{
使用::boost::regex;
使用::boost::regex_匹配;
使用::boost::smatch;
}
#否则
#包括
#恩迪夫
#包括
#包括
#包括
int main()
{
//打开输入文件
std::ifstream in(“file.txt”);
如果(!in.is_open())返回1;
//ECMAScript语法!
std::regex empty_或_comment_re(“\\s*(?:#*)”);
//注意:只匹配整数
标准::正则表达式值(“\\s*(\\s+)\\s+(\\s+)\\s+(\\d+)\\s*”);
//将包含结果
std::矢量结果;
size\u t lineno=0;//用于错误报告
std::字符串行;
//读台词
while(getline(in,line))
{
++利奈诺;
//匹配空行或注释行
如果(正则表达式匹配(行、空或注释)继续;
//包含数据的匹配线
std::smatch匹配;
如果(!regex_匹配(行、匹配、值匹配))
{

STR::CURR

可选地,对于@ mm的回答,您可以使用C++ 11的新<代码> <代码>功能。但是注意,目前不是所有的标准库都完全实现了这一点,所以如果需要的话,您可能也想退回到。

#include <fstream>
#include <iostream>
#include <sstream>

// note: Most C++11 regex implementations are not up to scratch, offer
// Boost.regex as an alternative.
#ifdef USE_BOOST_REGEX
#include <boost/regex.hpp>
namespace std
{
  using ::boost::regex;
  using ::boost::regex_match;
  using ::boost::smatch;
}
#else
#include <regex>
#endif

#include <string>
#include <tuple>
#include <vector>

int main()
{
  // open input file
  std::ifstream in("file.txt");
  if (!in.is_open()) return 1;
  // ECMAScript syntax!
  std::regex empty_or_comment_re("\\s*(?:#.*)?");
  // note: only matches integers
  std::regex values_re("\\s*(\\S+)\\s+(\\S+)\\s+(-?\\d+)\\s*");
  // will contain the results
  std::vector<std::tuple<std::string, std::string, int> > results;
  size_t lineno = 0; // for error reporting
  std::string line;
  // read lines
  while (getline(in, line))
  {
    ++lineno;
    // match empty or comment lines
    if (regex_match(line, empty_or_comment_re)) continue;
    // match lines containing data
    std::smatch match;
    if (!regex_match(line, match, values_re))
    {
      std::cerr<< "ERROR: malformed line in file.txt, line " << lineno
        << ".\n";
      return 1;
    }
    // read integer from match
    int n;
    std::istringstream iss(match[3]);
    iss >> n;
    // append to results
    results.push_back(std::make_tuple(match[1], match[2], n));
  }
}
#包括
#包括
#包括
//注意:大多数C++11正则表达式的实现都不是最新的
//Boost.regex作为替代方案。
#ifdef使用_BOOST_REGEX
#包括
名称空间标准
{
使用::boost::regex;
使用::boost::regex_匹配;
使用::boost::smatch;
}
#否则
#包括
#恩迪夫
#包括
#包括
#包括
int main()
{
//打开输入文件
std::ifstream in(“file.txt”);
如果(!in.is_open())返回1;
//ECMAScript语法!
std::regex empty_或_comment_re(“\\s*(?:#*)”);
//注意:只匹配整数
标准::正则表达式值(“\\s*(\\s+)\\s+(\\s+)\\s+(\\d+)\\s*”);
//将包含结果
std::矢量结果;
size\u t lineno=0;//用于错误报告
std::st