简体   繁体   English

需要在C ++中解析字符串的基本帮助

[英]Need basic help parsing a string in C++

C++ is not my preferred language. C ++不是我的首选语言。

I have a file that contains this: 我有一个文件包含这个:

e 225,370 35,75

I want to separate e, 225, 370, 35 and 75 from each other into a char and ints but I'm having trouble. 我想将e,225,370,35和75彼此分成char和ints,但我遇到了麻烦。 I tried doing everything I found online and in my C++ book and still it's not working out. 我尝试了在网上和我的C ++书中找到的所有内容,但仍然无法解决问题。 Please help. 请帮忙。

I would have an easier time doing this in Java. 我会更容易在Java中这样做。

The C++ String Toolkit Library (StrTk) has the following solution to your problem: C ++字符串工具包库(StrTk)具有以下解决方案:

int main()
{ 
   std::string data("e 225,370 35,75");
   char c1;
   int i1,i2,i3,i4;
   strtk::parse(data,", ",c1,i1,i2,i3,i4);
   return 0;
}

More examples can be found Here 更多例子可以在这里找到

If you have control over the format, it'll be (slightly) easier to read if you eliminate the commas, and just have input like 如果您可以控制格式,如果您删除逗号,它(稍微)会更容易阅读,并且只需输入

e 225 370 35 75 e 225 370 35 75

With this format, Poita_'s code for reading the data will work [edit: he's since update his code to explicitly read and skip the commas]. 使用这种格式,Poita_用于读取数据的代码将起作用[编辑:他自从更新他的代码以明确阅读并跳过逗号]。 Otherwise, you'll need to explicitly skip over the commas: 否则,您需要明确地跳过逗号:

char ingore1, ignore2;
char ch;
int i[4];

file >> ch >> i[0] >> ignore1 >> i[1] >> i[2] >> ignore2 >> i[3];

[Edit: if you're paranoid or really need to verify your input, at this point you can check that ignore1 and ignore2 contain commas.] [编辑:如果你是偏执或真的需要验证你的输入,此时你可以检查ignore1ignore2包含逗号。]

In most cases, however, the data are probably related, so you'll want to read an entire line into a single struct (or class): 但是,在大多数情况下,数据可能是相关的,因此您需要将整行读入单个结构(或类):

struct data { 
    char ch;
    int i[4];

    std::istream &operator>>(std::istream &is, data &d) { 
        char ignore1, ignore2;
        return is >> ch >> i[0] >> ignore1 >> i[1] >> i[2] >> ignore2 >> i[3];
    }
};

Having done this, you can read an entire data object at a time: 完成此操作后,您可以一次读取整个data对象:

std::ifstream infile("my data file.txt");
data d;

infile >> d;

Or, if you have a whole file full of these, you can read them all into a vector: 或者,如果你有一个完整的文件,你可以将它们全部读入一个向量:

std::vector<data> d;

std::copy(std::istream_iterator<data>(infile), 
    std::istream_iterator<data>(),
    std::back_inserter(d));

If you want to use the old fashioned C runtime 如果要使用旧式C运行时

FILE * pf = fopen(filename, "r");
char e;
int  a, b, c, d;
int ii = fscanf(pf, "%c %d,%d %d,%d", &e, &a, &b, &c, &d);
if (ii < 5) 
   printf("problem in the input file");
fclose (pf);

edit: added error checking based on comment from dreamlax 编辑:根据来自dreamlax的评论添加错误检查

Assuming that you've read the data into a strings ... 假设您已将数据读入字符串......

  1. strchr is like String.index. strchr就像String.index。
  2. strtol is like Integer.parseInt() strtol就像Integer.parseInt()

What else do you need? 你还需要什么?

#include <fstream>

/* ... */

ifstream file;
file.open("yourfile.txt");
char c, dummy;
int i[4];
file >> c >> i[0] >> dummy >> i[1] >> i[2] >> dummy >> i[3];
file.close();

Use Boost Tokenizer to split the string. 使用Boost Tokenizer拆分字符串。 I am assuming that only the first token is a char, so sample code would be something like: 我假设只有第一个令牌是char,所以示例代码如下:

#include <iostream>
#include <boost/tokenizer.hpp>
#include <string>
#include <vector>

using namespace std;

...

typedef boost::tokenizer<boost::char_separator<char> > tokenizer;

string teststring("e 225,370 35,75");
boost::char_separator<char> separators(", ");
tokenizer tokens(teststring, separators);
vector<string> substrings;
for (tokenizer::iterator iter = tokens.begin(); iter != tokens.end(); ++iter)
{
    substrings.push_back(*iter);
}

and, voila, you have all of your substrings in a neat vector. 瞧,你把所有的子串都放在一个整齐的矢量中。 The char is in substrings[0] as a std::string, and the following int values are in substrings[1] and those following, also as std::string. char在子字符串[0]中作为std :: string,并且以下int值在子字符串[1]和后面的那些中,也作为std :: string。 You will need to convert them to integer values. 您需要将它们转换为整数值。 For this I suggest you look at stringstream. 为此我建议你看看stringstream。

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
        ifstream f("a.txt"); // check for errors.

        char ch,dummy;
        int i1,i2,i3,i4;

        f>>ch>>i1>>dummy>>i2>>i3>>dummy>>i4;

        cout<<ch<<endl<<i1<<endl<<i2<<endl<<i3<<endl<<i4<<endl;

        return 0;
}

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

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