T
T
Trimsky2020-11-10 15:27:31
C++ / C#
Trimsky, 2020-11-10 15:27:31

How to split a file by hyphenation?

This code should split on \n

//Скажем s равен файлу test.txt:
   /*
  1
  12
  123
  */
    std::string separator = "\n";
    auto start = 0U;
    auto end = s.find(separator);
    while (end != std::string::npos)
    {
        std::cout << s.substr(start, end - start) << std::endl;
        start = end + separator.length();
        end = s.find(separator start);
    }


But it just outputs 1.
If that's the case, then how do you split the line at breaks in a way other than \n?
Tell!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-11-10
@jcmvbkbc

Is it okay that you have separator on top, and delim in the loop?
If you ask a question about the code, it makes sense to attach the entire code, in the form in which it raises the question.

But it just outputs the entire file.

Well, actually, your code should output the entire file, only line by line. Replace
std::cout << s.substr(start, end - start) << std::endl;

for example on
std::cout << s.substr(start, end - start) << ", " << std::endl;

for variety?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question