CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2019
    Posts
    56

    Question How to detect a text in output?

    For example: I get output like this (2nd below) and I want to output
    HTML Code:
    cout << new8.xml <<endl;
    after. Is it possible?
    HTML Code:
    <file>new0.xml</file>
        <file>new1.xml</file>
        <file>new2.xml</file>
        <file>new3.xml</file>
        <file>new4.xml</file>
        <file>new5.xml</file>
        <file>new6.xml</file>
        <file>new7.xml</file>

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to detect a text in output?

    Quote Originally Posted by prako2 View Post
    For example: I get output like this (2nd below) and I want to output
    Code:
    cout << new8.xml <<endl;
    after. Is it possible?
    Code:
        <file>new0.xml</file>
        <file>new1.xml</file>
        <file>new2.xml</file>
        <file>new3.xml</file>
        <file>new4.xml</file>
        <file>new5.xml</file>
        <file>new6.xml</file>
        <file>new7.xml</file>
    Did you already try it?
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2019
    Posts
    56

    Re: How to detect a text in output?

    No, because I don't know how to achieve this. But I probably could if I know how to find a file with the biggest number.

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to detect a text in output?

    Quote Originally Posted by prako2 View Post
    No, because I don't know how to achieve this.
    "achieve" what? Output?
    Then make first the "second" output, and then the "first" one!
    Victor Nijegorodov

  5. #5
    Join Date
    Aug 2019
    Posts
    56

    Re: How to detect a text in output?

    This is not exactly what I want. I want to find a file with the biggest number, increment it by 1 and then output.

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to detect a text in output?

    Quote Originally Posted by prako2 View Post
    ... I want to find a file with the biggest number, increment it by 1 and then output.
    And with what part of this task do you have a problem? And what is this problem?
    Victor Nijegorodov

  7. #7
    Join Date
    Aug 2019
    Posts
    56

    Re: How to detect a text in output?

    This:
    Quote Originally Posted by prako2 View Post
    But I probably could if I know how to find a file with the biggest number.

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to detect a text in output?

    Well, you could use CFileFind class to perform file searches (usually, among some file types).
    Then you open every file and read it content up to the end to find the "biggest number" and save this number together with the file name in some buffer.
    Now (after the search with CFileFind completed) you choose the biggest number and the corresponding file name.
    Victor Nijegorodov

  9. #9
    Join Date
    Aug 2019
    Posts
    56

    Re: How to detect a text in output?

    Thanks, but I want to do it directly from output text.

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to detect a text in output?

    What do you mean by "directly from output text"?
    Victor Nijegorodov

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to detect a text in output?

    I think he means something like this:

    Code:
    const string out = "<file>new0.xml</file>
        <file>new1.xml</file>
        <file>new2.xml</file>
        <file>new3.xml</file>
        <file>new4.xml</file>
        <file>new5.xml</file>
        <file>new6.xml</file>
        <file>new7.xml</file>";
    
    int hghnum = getHighnum(out);
    where in this case hghnum would be 7
    ??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to detect a text in output?

    Wow 2kaud! You definitely have a crystal ball in your hands!
    But I don't...
    Victor Nijegorodov

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to detect a text in output?

    If my crystal ball gazing is somewhere near the mark, then consider:

    Code:
    #include <string>
    #include <iostream>
    #include <charconv>
    #include <algorithm>
    using namespace std;
    
    size_t getHighnum(const string& out, const string& pre)
    {
    	size_t maxnum = 0;
    	const auto strt = out.data() + pre.size();
    
    	for (size_t off = 0; (off = out.find(pre, off)) != string::npos; off += pre.size()) {
    		size_t num = 0;
    
    		from_chars(strt + off, strt + off + out.size(), num);
    		maxnum = max(maxnum, num);
    	}
    
    	return maxnum;
    }
    
    int main()
    {
    	const string out = R"(<file>new0.xml</file>
    		<file>new1.xml</file>
    		<file>new2.xml</file>
    		<file>new3.xml</file>
    		<file>new4.xml</file>
    		<file>new5.xml</file>
    		<file>new6.xml</file>
    		<file>new7.xml</file>)";
    
    	size_t hghnum = getHighnum(out, "new");
    
    	cout << "Highest number found is " << hghnum << endl;
    }
    Last edited by 2kaud; September 5th, 2019 at 09:12 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to detect a text in output?

    @OP Hmm, invalid xml, not off to a good start.

  15. #15
    Join Date
    Aug 2019
    Posts
    56

    Re: How to detect a text in output?

    Thank you 2kaud, very useful.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured