I want to directly push a character array into the standard input stream stdin, but cannot think of a syntax that works. The closest I can think of is
freopen("input.txt", "r", stdin);
which reads the contents from a file "input.txt" into the FILE pointer stdin. But I don't like this approach because 1) it relies on creating an additional file, 2) I have to worry about creating one file for each of such requests, which can turn into a ton of txt files in a folder just for this simple purpose of assigning some character array to stdin.
Is there a better, more elegant way of doing this?
ungetc(). Some systems (IIRC, the older Unix systems like Solaris, HP-UX, AIX) have very limited pushback — the standard only guarantees one byte and they adhere to a limit close to that. Many modern systems (Linux, macOS) give you significant buffers — up to about 4 KiB under testing, and I didn't push it beyond that. Seeungetc()— number of bytes of pushback? for the details (from a few years ago, but the test code is in my answer there).freopenorungetcor what have you.