In a Makefile, what does ">" mean? - c

for example: ./main 45 M > mul1.out, in this situation does > mean save the output of the program to a file called mul1.out or does it use mul1.out as the input of the program?
I believe > means it saves the output of the program to the file

Using the > symbol, you are writing the output of the ./main program to a file mul1.out instead of the stdout.
Read more on input/output redirection in Linux.

Related

Is there a way to redirect a file without using its extension in Unix?

For example I have a C program with executable target prog and a file file.txt
Is there a way in my C program to make stdin to read and accept the file.txt without its extension. Currently my code to read the stdin is..
while (scanf("%d ", &n) != EOF)
For this I would have to do ./prog < file.txt
But I want to do this ./prog < file
But I want to do this ./prog < file
No, that won't work. You'll just have to be explicit and use:
./prog < file.txt
Change your program so that instead of redirecting using the OS, you take the file as a command line argument and open+read it yourself.
That way you can do whatever string manipulation on the file name you think is necessary.

Passing a Command Line Argument and a textfile to a program

I have a Read and Reverse Coding assignment where I need to pass a Textfile and a character (-L, or -W), depending on whether the operator wants the textfile returned in reverse by lines or by words. (I should also note that the assignment requires that nothing is asked of the user during the code. It must be decided which variation is wanted in the command line.)
I don't need help with the code to reverse the lines or words, but do need help with understanding how to take in character and the textfile, then use them in the code. I've tried using the parameters (int argc, char *argv[]) on the main, but anytime I try to pass in just the -L the terminal either says Command not found or clang: error: argument to '-L' is missing (expected 1 value)
Also, when my teacher passes a textfile to a program he often uses a >. Can someone explain how to use this?
Ex. program.c > hello.txt
Then he would end up using that .txt in the program.
Consider this:
program -L < data.txt
or
program -W < data.txt
or
cat data.txt | program -L
The "-L" or "-W" will be in argv[1].
Good luck!
The idea of Passing a Command Line Arguement is the following
Argc: argument counter amount of "strings"(arguments) passed for execution.
Its always 1 or greater as the calling of the function is an argument.
Argv: argument vectors(pointers), is a pointer to each of the arguments received by the command line
Example of program call:
./myprogram -w
argc=2
argv will have two pointers to strings(char):
argv[0]= "./myprograms"
argv[1]= "-w"
Now with your problem
When excecuting a program via command line you have a lot of options amongst these:
1) One of these is to give the program input of a file(the file will be passed character by character to the standard input ending with an EOF or -1 -not an ascii character-)
These can be done by the follow way
./program.c < hello.txt
2)Redirect the output of the program to a file
./program.c > hello.txt
What you are looking to do is input a file while passing an argument this can be done the following way
./program.c < hello.txt -L
IMPORTANT: "< hello.txt" will NOT count as an argument so for this case the case the argc and argv will be the follow
argc=2
argv[0]="./program.c"
argv[1]="-L"
Hopes this helps comment if you need anymore help or something isn't clear. Good luck with your course!!!

Building C file in Sublime Text 3 and using fork();

(running on a mac) My C.sublime-build file looks like this:
{
"cmd" : ["gcc -Wall -g $file_name -o ${file_base_name} && ./${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}
and I have a simple program with the following code:
#include <stdio.h>
#include <unistd.h>
int main ( int argc, char *argv[] ) {
printf("hi\n");
fork();
printf("bye\n");
return 0;
}
and sublime will execute it and give me
hi
bye
hi
bye
while executing from the shell gives me the correct result,
hi
bye
bye
why is this happening?
According to ISO C:
Standard input and standard output are fully buffered, unless they
refer to a terminal device, in which case, they are line buffered.
When you're using ST3, it does not refer to a terminal device so it is fully buffered. It means hi\n and bye\n will be stored in buffer zone and fork()will copy them to child process. Then both of them will be output twice.
When you're using the shell, you're using a terminal device and it is line buffered. During thr execution, hi\n will be output firstly and buffer zone is flushed due to the \n. Then bye\n is send to buffer zone and will be output twice.
It may be that when sublime executes it that stdout, for whatever reason, is not using line buffered output but fully buffered output instead. So, when you fork() the child, the "hi\n" still resides on the child's FILE too. The output of both is only flushed when the programs exit and they both print the same output.

Fprintf functions in c programs that arent redirected with stdout

I've created a C program in Unix where fprintf is declared like this:
#Include<stdio.h>
...
fprintf(stdout,"Example\n");
The problem is when I try to redirect stdout to another file like this:
$./ExampleProgram > file.txt
The expected result is to have is Example inside the file, but instead the output is like this one (I type on command promt lines 00 and 02 below):
00. BASH4.1$./ExampleProgram > file.txt
01. Example
02. BASH4.1$cat file.txt
03. BASH4.1$
If I redirect the stderr the output is like this one:
00.BASH4.1$./ExampleProgram &> file.txt
01.BASH4.1$ cat file.txt
02.BASH4.1$
There's no output at all
Any idea why this is happening?
EDIT: The problem may be the Buffer of the fprintf but I've tried to fix it using fflush(stdout) but it doesn't work
EDIT2: Added more details.
I have just tested the example you've listed above. It worked just fine.
Here's the code for the example test c-file:
#include <stdio.h>
int main()
{
fprintf(stdout,"Example\n");
return 0;
}
compile with : gcc example.c -o example
I used bash environment to compile and run.
Maybe you forgot to include stdio.h ?

Redirecting output of a C program as input of another program in Linux command shell

I wrote a program p1.c which takes input from the linux command shell (Using- char n=argv[1]). I want the character output of p1.c to be taken as input of program p2.c . How can I do this? I used the command
./p2.out < ./p1.out T > output.txt. It doesn't seem to work as 'T' is taken as input for p2.out and its output is written in output.txt.
Use pipeline: ./p1.out T | ./p2.out

Resources