[try Beta version]
Not logged in

 
How to detect a text in output?

Sep 5, 2019 at 11:43am


For example: I get output like this (2nd below) and I want to output

cout << new8.xml <<endl;

after. Is it possible?

1
2
3
4
5
6
7
8
<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>


If yes, how it can be done?
Sep 5, 2019 at 11:49am
You want to output
cout << new8.xml <<endl;
?

cout << "cout << new8.xml <<endl" << endl; will output that.

http://cpp.sh/93ro5
Sep 5, 2019 at 11:54am
I mean, I want to find a file with the biggest number, increment it by 1 and then output.
Sep 5, 2019 at 12:01pm
So, just to be clear, you want to:

1) Find the names of all files in directory which conform to a pattern, where that pattern includes a single number

2) Of those filenames, identify the one that contains the highest number

3) Generate a new filename which follows the same pattern, with a number one higher than the number you found in step 2

4) Output that new filename

Is that correct?
Sep 5, 2019 at 12:08pm
Yes, but you can skip 1).
Last edited on Sep 5, 2019 at 12:08pm
Sep 5, 2019 at 12:20pm
So, if we assume that you already have a list of strings containing the filenames you'll need to:

- Iterate over the strings in that list

- For each filename:
- identify the number. This should be easy - find the position of the . in the string, and the number is the part of the string that begins with the 4th character, and ends before the position of the .

- Keep track of the highest number found so far, and update it if the number you've just found is higher than the the previous value

- Once you've iterated over all the filenames, you'll have your highest number. Add one to it.

- Create a new string, containing a new filename which contains your new number

- Output that new filename
Last edited on Sep 5, 2019 at 12:20pm
Sep 5, 2019 at 12:29pm
Last edited on Sep 5, 2019 at 12:29pm
Sep 5, 2019 at 6:22pm
Serial cross-poster is still at it!

And he's just as clueless at the other sites he spams.
Sep 5, 2019 at 6:25pm
There's really nothing wrong with seeking as many opinions on as you can get. New programmers especially do not have the knowledge to judge the quality of the responses they get. Only by asking and exploring does someone begin to be able to choose whom they prefer. And even for the first few years cross-posting questions feels like a winning strategy.

For the poster, the only downside is when people get mad at them for the multiple posts.

Now, if someone multiple-posts (same site or different) because they don't like the (good) advice given them, either here or elsewhere, then I'll just ask what was wrong with the advice already offered.
Sep 5, 2019 at 6:28pm
if you can assume the highest file number has the highest create or update timestamp, you can use that to bypass iteration.

then its just
- parse this file name
- make new name with +1
Last edited on Sep 5, 2019 at 6:29pm
Sep 5, 2019 at 8:49pm
Only if you can guarantee no one will EVER touch any of the files, else you will have data loss. Searching the directory doesn't take long.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <algorithm>   // max()
#include <filesystem>  // directory_iterator
#include <fstream>     // ofstream
#include <iostream>
#include <regex>       // regex, smatch
#include <string>

int main()
{
  // Find the largest integer suffix value in files named like "new23.xml"

  long highest_suffix = -1;
  {
    std::regex pattern{ R"((new(\d+)\.xml))", std::regex_constants::icase };
    for (const auto& p : std::filesystem::directory_iterator( "." ))
    {
      std::smatch matches;
      auto filename = p.path().filename().string();
      if (std::regex_match( filename, matches, pattern ))
      {
        highest_suffix = std::max( highest_suffix, std::stol( matches[2].str() ) );
      }
    }
  }
  
  // Add a new file with the next suffix
  
  std::ofstream( "new" + std::to_string( highest_suffix + 1 ) + ".xml" );
  
  // All done!
}

Phones are a pain to use...

Anyway, I hope this helps.
Topic archived. No new replies allowed.