1

I know how to read input from a file, and I know how to use a strtok to read user input, but how would I go about combining the two?

For example, I want to read the following two lines from a text file:

00003, 3342, 54329
02425, 4323, 43255

Storing 00003, 3342, 54329, 02425, 4323, 43255 as separate variables.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • 3
    You don't use `strtok`. You use `fscanf` or something similar or you read the entire line and then parse it using `strtok`. Search here for `[c] parse text line from file`; there are literally hundreds of existing questions (and answers). – Ken White Dec 02 '15 at 02:26
  • Parsing.... That's the word I was looking for. I think I understand what to do – jahir anderson Dec 02 '15 at 02:38
  • 1
    Maybe you should show us how you'd read a line of input from a file, and how you'd use `strtok()` to split up a string — because if you know how to do those, the connecting link is making sure that you use `strtok()` on the string that holds the line of input. In general, we prefer to help those who have shown enough effort and initiative to have some code that shows what they've tried and can articulate where they're having a problem. – Jonathan Leffler Dec 02 '15 at 02:50
  • 3
    Any question starting with "how to I utilize strtok to do X" is basically guaranteed to be an example of an XY question. The right question is: "how do I accomplish X", and a good answer will *never* involve using `strtok`. – Jerry Coffin Dec 02 '15 at 02:55

1 Answers1

-1

Don't use strtok, use std::regex .

Depending on the size of your file you can read it either line by line or entirely in an std::string.

To read the file one can use :

std::ifstream t("file.txt");
std::string sin((std::istreambuf_iterator<char>(t)),
                 std::istreambuf_iterator<char>());

and to do the matching

std::regex word_regex(",\\s]+");
auto what = 
    std::sregex_iterator(sin.begin(), sin.end(), word_regex);
auto wend = std::sregex_iterator();

std::vector<std::string> v;
for (;what!=wend ; wend) {
    std::smatch match = *what;
    V.push_back(match.str());
}
Community
  • 1
  • 1
g24l
  • 2,819
  • 11
  • 27
  • 2
    I think that using regexes for this is overkill when the data is a regularly formatted as the example data. – Jonathan Leffler Dec 02 '15 at 02:49
  • @JonathanLeffler in what way performance/complexity ? It is also nicer since you don't have to work with c style strings. – g24l Dec 02 '15 at 02:55
  • @JonathanLeffler but certainly strtok has a nicer / easier interface. – g24l Dec 02 '15 at 02:58
  • 1
    @g24l: It's too much overkill. Regexes are designed to be used to create complex matches against (presumabily) long strings. That's why they're *compiled*. Have you seen your C++ compiler being fast a single time (except for Hello World, maybe)? C strings **are not bad**, and they have their purpose and use, as do regexes and `strtok()`, and you're misunderstanding all of them three here. I don't see the regex code to be too readable either... – 3442 Dec 02 '15 at 03:00
  • 1
    I didn't say `strtok()` was better — I dislike `strtok()` intensely. I am suggesting that there are probably other ways to do it, and I'm not convinced that anything as heavyweight as a regex is needed. I also observe that the regex shown almost certainly does do what you want. The `\s` in the string is probably a typo for `\\s`; there is no evidence that the data contains one or more close square brackets in a row. – Jonathan Leffler Dec 02 '15 at 03:06
  • 1
    The use of a regular expression adds an unnecessary complication. Never use regular expressions unless you really need them, and there is zero need for it here. Don't use a sledgehammer when a simple screwdriver will work better. There's a famous quote about having a problem, adding a regex, and now having two problems; that quote was the result of people who write answers like yours. – Ken White Dec 02 '15 at 03:10
  • Unnecessary complication is added by all the intricacies of strtok, e.g. Not handling quoted strings which are common in csv. The STL way of doing this is with the regex. – g24l Dec 02 '15 at 08:41
  • @JonathanLeffler Thanks. I think there is nothing else than regex and strtok. Boost also has facilities but they are even more cubursome. The problem is that we are educating developers to use strtok. – g24l Dec 02 '15 at 08:47
  • @KemyLand I agree that they have their use, you can browse SO to just feel the pain strtok users are experiencing. Don't get me wrong , I am not a regex lover, but they are far better than strtok logic. There is no difficulty in compilation in using a simple regex as this is. – g24l Dec 02 '15 at 09:37