Question

I have an API function, which generate a new stdin input curse like >> . Because I have a lot of data already in memory in the form of array, how can I pass the data into the >> in C code? for example, getdatafromstdin is an API function, I do not know the details:

getdatafromstdin;
>>

This is normal form, but I do not want input data via keyboard, I want to pass data to >> by C code. Like

getdatafromstdin;
passdatatostdin;
Was it helpful?

Solution

There are very little information in the question to be able to give a proper answer. One option is fixing getdatafromstdin to get a FILE * as argument instead of assuming stdin, so you can give a FILE * that you want. Another option is to divide the program into two parts. One that generates the input data and the other that uses getdatafromstdin and then pipe the two.

One easy to add option however if you don't want to do the others is a bit of cheating. You can write the data you want to feed to stdin to a file and then use freopen to reopen stdin to read from that file. Then calling your getdatafromstdin would read from that file.

If you pipe in stdin or freopen it, making it use the terminal later is not trivial. So I would recommend the first option (fix the getdatafromstdin function to get a FILE * parameter), but my guess is that it's not your own API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top