I have output from my program.
I need it to be sent directly to Notepad or any other word processor.

How can I do that?

it's really important--thanks.

Dani AI

Generated

Short answer: write the output to a file, close/flush it, then launch an editor on that file. was right to point toward file I/O, and 's idea of launching Notepad is fine — but using the OS API to open the file is safer and more portable than calling system() directly.

Notepad is a GUI program and does not read from stdin, so piping (for example prog | notepad) will not work. Use a temporary or named file, ensure the file handle is flushed and closed, then open the file with the platform API. On Windows, ShellExecute is a simple way to open the file with the default text editor (or to run Notepad explicitly). CreateProcess or ShellExecute also avoid command-line injection risks that come with system().

Example (Windows) — open the written file with the default handler:

#include <windows.h>

bool OpenFileWithDefaultApp(const char* path)
{
    HINSTANCE result = ShellExecuteA(NULL, "open", path, NULL, NULL, SW_SHOWNORMAL);
    return (INT_PTR)result > 32; // >32 indicates success
}

Practical tips and gotchas:

  • Always close the file before launching the editor or the file may appear empty.
  • For temporary files use GetTempPath/GetTempFileName (Windows) or mkstemp/tmpfile (POSIX).
  • Notepad historically needs a BOM to detect UTF-8 correctly; add the UTF-8 BOM (0xEF,0xBB,0xBF) or write UTF-16LE with BOM if non-ASCII text is required.
  • Prefer ShellExecute/CreateProcess over system() to avoid injection and quoting problems.
  • Cross-platform: macOS uses open, Linux desktops use xdg-open (or desktop-specific helpers) to launch the default viewer — run those via safe APIs or carefully constructed commands.

These steps make the approach robust across environments while addressing the common pitfalls discussed in the thread.

Recommended Answers

All 2 Replies

Including the library will give ye' the tools you need to read and write to a file. Specifically, creating an object of type 'ofstream' will allow you access to library functions that write to a file.

And after writing the output to a file call system() (or some other similar os-specific function) to launch Notepad, for example

system("Notepad myfile.txt");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.