简体   繁体   中英

Need basic help parsing a string in C++

C++ is not my preferred language.

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. I tried doing everything I found online and in my C++ book and still it's not working out. Please help.

I would have an easier time doing this in Java.

The C++ String Toolkit Library (StrTk) has the following solution to your problem:

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

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]. 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.]

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:

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

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 评论添加错误检查

Assuming that you've read the data into a strings ...

  1. strchr is like String.index.
  2. strtol is like 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. I am assuming that only the first token is a char, so sample code would be something like:

#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. You will need to convert them to integer values. For this I suggest you look at 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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