So in my text "123456789#987654321" is written. And in C++ i got 2 int called numbers1 and numbers2. I want numbers1 to be 123456789 and numbers2 will be 987654321. Could you please tell me how can i do that?
Asked
Active
Viewed 62 times
2 Answers
2
You can try the code below.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
ifstream in_file;
in_file.open("your_text.txt");
if (!in_file.is_open()) {
fprintf(stderr, "Unable to open file!\n");
exit(1);
}
string text;
getline(in_file, text);
size_t first_end = text.find_first_of("#", 0);
string parsed1(text.substr(0, first_end));
size_t second_begin = first_end + 1;
string parsed2(text.substr(second_begin, text.size() - second_begin));
int num1 = stoi(parsed1);
int num2 = stoi(parsed2);
return 0;
}

Joxixi
- 651
- 5
- 18
1
What you want is to split the text by '#'
look here How do I tokenize a string in C++?
and Parse (split) a string in C++ using string delimiter (standard C++)
for examples.

DataYoda
- 771
- 5
- 18