let's say there is a file. there are N lines in it and from N lines I have M lines of the form:

5 used hard 4 label 10 

I need to cram inta from this line, that is, 5, 4, 10. structure of the form:

 struck hard{ int fisrtvar; int secondvar; int thirdvar; } 

Closed due to the fact that the issue is too general for participants Vladimir Martyanov , Oceinic , aleksandr barakin , zRrr , Kromster Dec 1 '15 at 10:57 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What exactly is causing you difficulty? - Kromster
  • @KromStern in my view is to take a string and poke it with tokens or search for numbers. but for example, if we have large numbers, we need to somehow add them. - ParanoidPanda
  • Lots of options for how to do it. Search for spaces and use atoi, for example. - Vladimir Martyanov

2 answers 2

 #include <regex> std::regex pattern("^(\\d+)\\s+used\\s+hard\\s+(\\d+)\\s+label\\s+(\\d+)"); std::cmatch match; regex_match(input_c_string, match, pattern); if (match.size() == pattern.marc_count() + 1) { mystruct.firstvar = stoi(match[1]); mystruct.secondvar = stoi(match[2]); mystruct.thirdvar = stoi(match[3]); } else { std::cerr << "string does not match" << std::endl; // Тут должна быть Ваша обработка неподходящей строки } 

Something like this. http://www.cplusplus.com/reference/regex/basic_regex/

IMHO, write your parser (although it is not complicated here) - reinvent the wheel, which has long been in the STL


On pure C (checked, working) add the third number by analogy:

 #include <stdio.h> #include <stdlib.h> typedef struct { int f; int s; int t; } Hard; int parse_string (const char *s, Hard *h) { int i = 0; int j; while (s[i] >= '0' && s[i] <= '9') ++i; if (i == 0 || s[i] == '\0') return 0; char *buffer = (char*) malloc(i + 1); for (j=0; j<i; j++) buffer[j] = s[j]; buffer[i] = '\0'; h->f = atoi(buffer); free(buffer); while((s[i] < '0' || s[i] > '9') && s[i] != '\0') ++i; if (s[i] == '\0') return 0; int snd_begin = i; while (s[i] >= '0' && s[i] <= '9') ++i; if (s[i] == '\0') return 0; buffer = malloc(i - snd_begin + 1); for (j=snd_begin; j<i; j++) buffer[j-snd_begin] = s[j]; buffer[i-snd_begin] = '\0'; h->s = atoi(buffer); free(buffer); // То же для третьего числа return 1; } int main(void) { const char s[] = "22 xxx yyy zzz 33 www"; Hard h; if (parse_string(s, &h)) { printf("%d %d\n", hf, hs); } else { printf("doesnt match\n"); } return 0; } 
  • I need a bike on clean lines - ParanoidPanda
  • @ParanoidPanda, then you need to remove the C ++ label. - andy.37
  • Um, std :: regex was fixed and became usable only in gcc 5. And I don’t even know about MSVS. - Monah Tuk
  • @MonahTuk MSVS flash drive In Qt, QRegExp works fine. I basically do not understand how it is possible to insert some "windows.h" in a cross-platform language - andy.37
  • @ andy.37, and what in MSVS without windows.h does not work? - jioev

On pure C, use regex.h (POSIX regulates) or just strtol ( http://www.opennet.ru/cgi-bin/opennet/man.cgi?topic=strtol&category=3 ):

 #include <stdio.h> #include <stdlib.h> int main(void) { const char* str = "5 used hard 4 label 10"; long value; while (*str) { char *enptr = NULL; value = strtol(str, &enptr, 10); if (str == enptr) { str++; continue; } else { str = enptr; } printf("value: %d\n", value); } return 0; } 

or link to ideone: http://ideone.com/w4Ke4Y

For your task, correct it yourself.