directing file output to the console - c

I'm working with a C-function foo(FILE* file) that outputs some text to a file.
Rather than write to a .txt file, I'd like foo to write to the console.
Is there a way to pass 'the console' as a FILE*? (And if so, how) ?
(foo is part of a library and I can't edit the source code directly)

There's a special file object called stdout that will write to the console (assuming the shell didn't redirect anything).
foo(stdout);

Related

Is there any way of storing a command written in prompt into JSON file

I am using ubuntu and i have used a library named as ICLI (interactive command line interface) which lets one build his own prompt. and i want whatever command i write in this prompt to be stored as JSON.
One way of doing so is file write, for which you can try
with open("data_file.json", "w") as write_file:
json.dump(data, write_file)
Here, data is a variable created before writing these lines. data contains whatever information needs to be stored, in JSON format.

Can I pass text file as an argument in a main function in C?

I am wondering if I could pass file as an argument in a main function? I mean not a name of the file but file itself.
Not unless something external (e.g. a bash script) reads in the file and adds it as an argument. If there is a binary 0 in the file, that would be interpreted as the end of string.
You can achieve something similar using input redirection, where the contents of a file is redirected to stdin, e.g.
myprogram < myTextFile
You would then be able to read the contents of the file by reading from stdin.
You could, as long as there's no 0 bytes in the file.
Also, you shouldn't.
If you want to know how to do that, it depends on the operating system.

fprintf() present in a C .exe file called from MATLAB doesn't work

I'm calling a C executable compiled using Cygwin in MATLAB, using the unix() function. This works fine, and I can see the desired output on the MATLAB command window. However, there is an fprintf() inside the executable that is supposed to create and write to a text file which does not run - no such file is created. The text file is created just fine when I run the executable directly through Cygwin.
I was wondering if I need to grant permissions to the MATLAB file/executable to enable this? How could I go about this?
What path are you using to create the file? It might have been created -- just not where you expected it.
If it's a relative path, you could use getcwd(2) inside your C program to get and print the working directory (or e.g. getpid(2) to get the PID and then do ls -d /proc/<pid>/cwd, which will work on Linux at least). Once you have the working directory, check if the file is somewhere in there.
If it looks like the file really isn't being created, my next step would be to add some error checking to functions and print messages for errors to try to figure out what's going on. strerror(3) and perror(3) might come in handy.

Read file to standard in for parser

I am trying to implement a shell program in a linux environment. The part I am having trouble with is reading a setup_file inside of a shell before running the shell, to do things like set environment variables.
Currently the shell has a parser_results = parse() function which does a "getchar" and waits until the user types something into stdin, then does an execute(parser_result) which executes the command using the output of the parser.
What I want to do is to read the setup_file which has commands inside of it, have the parser read them in and give me the data structures I need. Then I can run execute.
My question is is how do I redirect the contents of the file to stdin? And how do I call the parser to parse this redirected input? I have been playing with dup and dup2 to no avail.
Short answer (to the question 'how do I redirect the contents of the file to stdin') is "You Don't".
You revise your input function to read from a given file stream instead of stdin, and then for reading from the file, you open it and pass that file pointer to your parsing code (and close when the parsing code is done), and then when you're ready for user input, you call the parsing code with stdin instead of the file. That saves fiddling with stdin.

Additional console window

I have application that has to write to console. Also I call function from dll that writes to stdout too. Now I want to have separate output window to get all output from dll there. (So I will have 2 console windows: one for client app, one for dll) How to make it? I use windows+mingw
You can't. stdout is a single file handle and if both your application code and the dll are writing to it, they're going to go to the same place no matter what you do.
Your best options would be to either:
change your application code to write to something besides stdout (i.e stderr or a file), or
change your application code so that its output statements are 'tagged' in a way that you can filter using a tool like grep or sed/awk

Resources