开发者

Reading text fields from file with custom separator

I am working on a problem for a class I'm taking in which we need to read in text from a file to a 2d table of strings (called 'string table[][]'). The text file I'm reading in is formatted as follows:

Ain el Beida # - # - # OEB # Algeria # Africa # F # 42578 # 61997 # 90560 # #

Segbana # - # - # ALI # Benin # Africa # F # -1 # 10219 # -1 # #

Skelmersdale # - # - # LAN # England # Europe # F # 42611 # 42104 # 39279 # #

#

As you can see, each field is separated by a '#', the end of a line is denoted by 2 #'s, and the end of the file with 3 #'s. I've been looking at a few different ways of isolating each field so that I can save it to the array, but so far have not found anything that works well for my purpose.

I've been banging my head against this for a few hours now and I would really appreciate any开发者_开发知识库 advice on how to go about getting this to work.


Consider using std::getline, since it allows you to specify a delimiter (in your case, the delimiter is #).

std::ifstream file("somefile.txt");
std::string field1;
std::getline(file, field1, '#'); // Ain el Beida

Note though that each field is actually separated by a space and a #, so you will have leading / trailing whitespace in some cases.

Since this is for a class, I'll let you figure out the rest!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜