System() function in C showing buggy output - c

I am using the system() function in C to run a system command. While using the system(), the command I am using is:
system("C:\splint-3.1.2\bin\splint first.c>output.txt");
However, the output of this is not being correctly sent to the txt file. To be more specific, the txt file is created, however the output is not appended to the file.
On running the same command from the CMD, the output is correctly sent to the txt file.
Any idea on what's going wrong?

Escape your backslashes so the compiler interprets them correctly:
system("C:\\splint-3.1.2\\bin\\splint first.c>output.txt");

Related

How to redirect output to a file in C?

I am trying to, in C:
Read data from a file
Manipulate the data
Write manipulated data to another file
In the assignment requirements, it says to compile and run the program with the following commands:
gcc -o name name.c
./name inputFileName.ext > outputFileName.ext
I am unfamiliar with the " > " command. I have a couple of questions:
Online, it says that " > " redirects command output to a file, and I'm not sure exactly what "command output" means. I'm redirecting the output from my name.c file to the outputFileName.ext file. Does command output mean stdout? If so, which C keyword would I use to write information to the outputFileName.ext file from name.c as stdout?
When I open and read my input file, I need to access the file that was passed in from the command line. Does the " > " character count as another command line argument? Can I still access inputFileName.ext from main() with the statement " argv[1] " ?
Online, it says that > redirects command output to a file, and I'm not sure exactly what "command output" means.
"command output" refers to the stdout (Standard Output) stream of the program.
Do note that some shell commands are not separate programs but are actually shell builtins, though they'll still support output redirection. On Windows, most shell commands (like dir and del) are built-ins whereas on Linux/BSD/etc most shell commands are separate programs (like ls and mkdir)
If your program calls puts( "foobar" ); then running ./name from Bash will display "foobar" in your terminal emulator. But if you run ./name > file.txt then the "foobar" text will be written to file.txt and it will not be displayed in your terminal emulator.
Try it with the ls command, for example: ls -al > files.txt. This works on Windows too (dir /s > files.txt).
I'm redirecting the output from my name.c file to the outputFileName.ext file. Does command output mean stdout?
Yes.
If so, which C keyword would I use to write information to the outputFileName.ext file from name.c as stdout?
You don't. This is a shell/OS feature and is not part of C.
Let's clarify a few things:
>, < and a few other symbols (that are not relevant to your question) are control operators for your command line interpreter (a.k.a the shell). When the shell sees any of those, it assumes the command line arguments to your program are now finished. So in your case, your program will have argc=2 and argv = ["name ", "inputFileName.ext"].
The "redirection" thing means that whatever your program would normally write to the screen via the stdout (which is ulitized by default when calling printf() putchar(), puts()) will be written to the filename that comes after >. Your pogram is completely unaware of this fact. In your code, you should just assume you are printing on the screen. It is the responsibility of the one who executes the command to perform the redirection. (Also: "outputFileName.ext" does not need to exist, it will be created if it doesn't, but the redirection will override anything previously written in that file, so take extra care not to redirect to a .c file by accident or to your results of your previous execution, if you need them both)
< (not in your question, but closely related) works the opposite way around as you would imagine, with the program reading input from that file rather than from the keyboard. (obviously the file needs to exist now)
For the second part of your question, you can (and should) still access the name of the input file via the contents of argv[1]. You will open the file and read from it via some of the C functions that takes a file descriptor as an argument (like fscanf(), fgets(), getline()).
Finally, are you sure the command given to you is
./name inputFileName.ext > outputFileName.ext
and not
./name < inputFileName.ext > outputFileName.ext
?
The latter uses redirection both for input and for output, and you should not do anything different when reading, just read normally from stdin.

printf() redirection to file in command line (Cygwin)

I've got a C program that's supposed to work on command line interface taking few arguments - to do that I'm using Cygwin.
I want to know if it's possible to printf some prompts into command like - things like "Give a number:" before I get data (program needs to prompt for a couple of things) and get the final result redirected to file. Or if I'm redirecting to file I need to just resign myself to the fact that I can't print anything and everything goes into the file?
Essentialy I'd like something like this:
printf("Please enter any number:");//prompting for some variable
scanf("%d", &some_variable);
function_printing_its_output();//a nodescript function that at the end uses printf() to get results on screen
Now I was wondering if it's possible to make the first printf() prompt appear in command lie (possibly using different function if it exists) but have the function output go into the file (and I cannot directly make it print to file, the program needs to use ./program_name some_stuff file1 > file2 format to work redirecting to file2).
You could have your prompt go to stderr using fprintf(stderr, "...");.
– lurker

Store output of C program in shell

I have a C program which gives some output. I am compiling the C program via the shell but I need the output from the run C program and store in shell.
Edit.
Save the output to a shell Variable.
I assume that you want to store the output of the program in a variable. Unix shells offer a facility that's called command substitution to do just that. Depending on your shell, you can do either :
output=$(./run)
or
output=`./run`
Bash supports both. If, however, you want to save the output to a file, you will need to redirect the standard output stream to a file. You can do this like this :
./run > output.txt
Or, if you want to see the output while the program is running and save it to an output file as well, you can use the tee utility and pipe the output of your program to it.
./run | tee output.txt
You can redirect your output into a file like this:
./run > file
If you want to store it into a variable, you have to decide which shell we're talking about. It depends whether you have a windows shell, or linux bash..

Transferring output of a program to a file in C

I have written a C program to get all the possible combinations of a string. For example, for abc, it will print abc, bca, acb etc. I want to get this output in a separate file. What function I should use? I don't have any knowledge of file handling in C. If somebody explain me with a small piece of code, I will be very thankful.
Using function fopen (and fprintf(f,"…",…); instead of printf("…",…); where f is the FILE* obtained from fopen) should give you that result. You may fclose() your file when you are finished, but it will be done automatically by the OS when the program exits if you don't.
If you're running it from the command line, you can just redirect stdout to a file.
On Bash (Mac / Linux etc):
./myProgram > myFile.txt
or on Windows
myProgram.exe > myFile.txt
Been a while since I did this, but IIRC there is a freopen that lets you open a file at given handle. If you open myfile.txt at 1, everything you write to stdout will go there.
You can use the tee command (available in *nix and cmd.exe) - this allows output to be sent to both the standard output and a named file.
./myProgram | tee myFile.txt

How to read output and give input to a program from c program?

This is with reference to the below question:
Execute program from within a C program
How do I do the same on Windows with Tiny C Compiler?
I need to execute a .exe fro c program and give input to it from within the same C program by using a file or string as source and read the output from it into a string or file.I have been using system() frunction. Any suggesstions or examples are welcome..Thanks in advance
The simplest way if you don't have popen() etc, or you want to avoid the complexity, is to simplly write a data file eg. infile with fwrite() execute the external program with system() and then read outfile.
system("prog.exe <infile >outfile")
Your prog.exe only has to read stdin and write stdout.
This way you can easily test it with the contents of in/out file.
You would normally do this in your tmp directory and then delete them when you are finished.
The only thing to be careful of is the path to the .exe
Google for "windows popen" and you find this link:
lists.trolltech.com/qt-interest/1999-09/thread00282-0.html
The C runtime library also has _popen(), but I would recommend against it.

Resources