How to input multiple lines code to terminal to pass to C++ variable

Need to few lines of code to C++ with terminal
Text
Text
text
How to proceed?
What exactly are you trying to do? Explain in more detail, please.
jNc wrote:
Need to few lines of code to C++ with terminal
Text
Text
text


1
2
#include <iostream>
int main() { std::cout << "Text\nText\ntext\n"; }


jNc wrote:
How to proceed?

Have another go at explaining your problem.
Last edited on
I have have programm output in terminal. I need to save it to txt file or copy somehow to manipulate in c++
I have have programm output in terminal. I need to save it to txt file or copy somehow to manipulate in c++


Open a cmd window and navigate to the folder with the executable (example.exe, say) in.

Then:
example.exe > filename.txt

This simply redirects the output, as in "save it to txt file".


If you aren't familiar enough with the cmd window to navigate to the relevant folder, then just use windows explorer to get there and type cmd (and press enter) in the location bar. It will dutifully open a command window already navigated to the relevant folder.
Last edited on
you can also pipe directly from one program into another without having to write a disk file. I think its the | symbol but you can look it up if that interests you. I prefer the text file, so I can see what the program was getting if there is an error.
How to pass below?
qtcreator_process_stub.exe > FILE.txt
This is an internal helper of Qt Creator. Do not run it manually.
Not Qt, but it works fine.
You display output in the terminal - and in the same time, you write it in your Log file. I hope that it helps you (use C++ 17) :

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <mutex>
#include <fstream>

namespace simpleLog {

    inline bool log(std::ostream& out) {
        return !!(out << std::endl);
    }

    template<typename T>
    bool log(std::ostream& out, T&& value)
    {
        return !!(out << std::forward<T>(value) << std::endl);
    }

    template<typename First, typename ... Rest>
    bool log(std::ostream& out, First&& first, Rest&& ... rest)
    {
        return !!(out << std::forward<First>(first)) && log(out, std::forward<Rest>(rest)...);
    }

    inline std::mutex logger_mtx;

    class log_stream {
    public:
        log_stream(std::string_view str, std::ostream& ifile) : txt(str), file(ifile)
        {
            std::string s{ "[" };
            txt = s + txt + "] ";
        }

        template <typename... Args>
        bool operator() (Args&&... args) {
            bool ok = log(file, std::forward<Args>(args)...);
            {
                std::lock_guard<std::mutex> lck(logger_mtx);
                log(std::cout, txt, std::forward<Args>(args)...);

                if (!ok)
                    log(std::cout, txt, "Error writing to log file");
            }

            return 0;
        }

    private:
        std::string txt;
        std::ostream& file;
    };
}

int main()
{
    std::ofstream file("log.txt");
    simpleLog::log_stream log("->", file);

    log("\nLast Night A DJ Saved My Life ", 777, "\nLast Night A DJ Saved My Life with a song");

    return 0;
}
Last edited on
Topic archived. No new replies allowed.