run 2 c programs in same time in mac console - c

In study course i need to use 2 programs in same time to writing strings in one file.
I typed two similar programs on C:
#include <stdio.h>
int main(int argc, char** argv)
{
FILE *f;
f = fopen("output.txt", "w+");
while (1)
{
fprintf(f, "%s", "kill me pls \n");
}
return 0;
}
and
#include <stdio.h>
int main(int argc, char** argv)
{
FILE *f;
f = fopen("output.txt", "w+");
while (1)
{
fprintf(f, "%s", " NO! \n");
}
return 0;
}
And then i compiled and tried to run this programs in same time, using command
./prog1 & ./prog2 &
But nothing happening. In console i saw:
stolz$ ./prog1 & ./prog2 &
[7] 3920
[8] 3921
How must i type shell command to run this programs in the same time?

How must i type shell command to run this programs in the same time?
The way you proposed in your question was the right way:
$ ./prog1 & ./prog2 &
[7] 3920
[8] 3921
That starts both of the programs running in the background at the same time.
What happens then: Your code opens the output.txt using fopen w+. man fopen tells us:
w+ Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at the
beginning of the file.
Lets change that to a+ and modify your code by adding a sleepand a fflush for it:
#include <stdio.h>
#include <unistd.h> # that also need this
int main(int argc, char** argv)
{
FILE *f;
f = fopen("output.txt", "a+"); # changed w+ to a+
while (1)
{
fprintf(f, "%s", "kill me pls \n");
fflush(f); # this for that:
sleep(1); # that
}
return 0;
}
Make the above changes to prog2.c also, compile them:
$ gcc -o prog1 prog1.c ; gcc -o prog2 prog2.c
and start them and tail the output.txt:
$ ./prog1 & ./prog2 &
[1] 6976
[2] 6977
$ tail -f output.txt
kill me pls
kill me pls
kill me pls
NO!
kill me pls
NO!
kill me pls
NO!
kill me pls

Related

What is ./nqt in C programming and how do i change file without changing them in the code

So i trying to input multiple files in the assigned program.
My input file code is as such:
int read_File(int *hp, int *d, int *s, char t[])
{
FILE *infile;
infile = fopen("input1.txt", "r");
if (!infile)
{
return 0;
}
else
{
fscanf(infile, "%d", hp);
fscanf(infile, "%d", d);
fscanf(infile, "%d", s);
fscanf(infile, "%s", t);
fclose(infile);
return 1;
}
i did
$>gcc assignedProgram.c -o nqt
$>./nqt input1.txt
but if i want to read input2.txt, i have to change from input1.txt to input2.txt in the codes. Is there anyway to bypass that and read input2.txt without changing from input1.txt to input2.txt in the codes
like when i tried ./nqt input1.txt => it's normal
BUT ./nqt input2.txt it's segmentation fault:11
I tried:
to change "input1.txt" in the codes to "nqt" but that was a dumb idea
and BTW: what is ./nqt
Please help me!
You must write main() as one of the equivalent forms below
int main(int argc, char **argv) { /*...*/ }
int main(int argc, char *argv[]) { /*...*/ }
so that argc and argv are set up by your environment to proper values.
For example
$ ./nqt input.txt
^^^^^ ^^^^^^^^^ --> 1
\\\\\-------------> 0
Translates in your program to
argc == 2
argv[0] ==> pointer to "./nqt"
argv[1] ==> pointer to "input.txt"
argv[2] ==> NULL
When it comes to nqt, it is the name of the program you specified with -o flag when compiling: gcc assignedProgram.c -o nqt. In order to run the program, you need to use ./ prefix, thus ./nqt means "run the program called nqt".
If you want to pass the name of the file as an argument, you should tell main function to accept command line arguments: int argc and char* argv[] (you can read about it here). Then the name of the file you will pass by running ./nqt <filename> would be stored in argv[1], which you should pass to read_File function as an argument.

Glob function gives only one result in C

I have to program a shell in C and need to handle globing in it and I am only allowed to use the function glob. But when I try to use it, it only gives me one result back.
#include <glob.h>
#include <stdlib.h>
int main(int ac, char **av)
{
glob_t s;
unsigned long i = -1;
if (glob(av[1], 0, NULL, &s))
printf("ERROR !\n");
else
while (++i < s.gl_pathc)
printf("%s\n", s.gl_pathv[i]);
return (0);
}
I run this code in a folder where there is two C files : replace_glob.c and test.c
And when I run this code :
$ ./a.out *.c
replace_glob.c
$
I dont understand why and I would really appreciate your help
In the command line
./a.out *.c
the shell expands the glob pattern, so your program sees
{"./a.out", "replace_glob.c", "test.c", NULL}
as its argv. You need to quote the pattern for the program to see it:
./a.out '*.c'

fopen() and command line arguments

I'm trying to write a program that will read the first character in a text file. If I use ./a.out myfile.txt it works as intended but if I use ./a.out <myfile.txt I get Segmentation fault: 11. The reason why I'm trying to include the <is because this what is in the spec of the assignment. The below code is just a simplified example that i've made that has the same issue:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int func(int argc, char **argv){
FILE *fp;
int test = 0;
fp = fopen(argv[1], "r");
fscanf(fp, "%i", &test);
printf( "current file: %s \n", argv[1]);
}
int main(int argc, char **argv){
func(argc, argv);
}
Is there any way I can get it to accept the argument as <myfile.txt?
No, nor should you try. Files redirected this way will appear at stdin and you should use that instead (hint: check argc).
If you want to use a file if specified, but otherwise stdin, use something like:
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = stdin;
In your command ./a.out <myfile you redirect stdin to myfile. This means reading from stdin is actually reading from myfile. So, in this case your argc == 1, so argv[1] you use to open is NULL (see main spec on its arguments). fopen crashes when uses NULL name.
You may do your utility in another way: always read stdin. When you need file to input do like this: cat myfile | ./a.out. This is very nice approach and worth considering.

How to display the output of a program both to a file and to the console(stdout)?

Say I have the following program that simply outputs "Hello World":
//DEMO.c
#include<stdio.h>
int main()
{
printf("HELLO World");
}
Now I want to display it both to the screen and to a file output.txt.So I enter the following command in the command prompt(I use CodeBlocks on Windows XP and have configured it to work on command prompt as well):
demo.exe>>output.txt>>stdout
It doesn't work!!! Please tell me how to do it,ie how to output the same thing that I see on my screen(When i run the program),simultaneously to a text file?
You will need to download a tee command for Windows. tee is a UNIX/Linux command that copies the standard input to standard output and also outputs to a file. Then, you can do this:
demo.exe | tee output.txt
Here is one port of tee for Windows.
#include <stdio.h>
#define my_fprintf(fp,...) do{fprintf(fp, __VA_ARGS__);fprintf(stdout, __VA_ARGS__);}while(0)
int main(int argc, char **argv){
FILE *fp;
fp=fopen("output.txt","w");//or filename from argv[1]
my_fprintf(fp, "hello world by %s\n", argv[0]);
fclose(fp);
return 0;
}

Reading a file name from piped command

So I'm trying to get the C program to read a filename from the command line in the following format:
cat (filename path) | (program name)
i can get it to read the name of the input file when its entered as a command line argument, but it won't read from the concatenated argument
here's the code, right now its reading the name of the file as if written after the program name on the command line.
#include <stdio.h>
#include <string.h>
//initialize file pointer
FILE *file;
//initialize global variables
#define DEFAULT_LEN 70
//main
int main(int argv, char *argc[]){
//open File for reading
file = fopen (argc[1],"r");
//test for failure of file open, if failed, print message, quit
if(file == NULL){
printf("I'm sorry Dave, I'm araid I can't do that.\n");
printf("Open the file \"%s\" that is.\n", argc[1]);
return(0);
}
//read the first line of a file into an array
char temp[DEFAULT_LEN]; //where the read data is put
fgets(temp,DEFAULT_LEN,file); //stops at DEFAULT_LEN on \n
//print out temp
printf("%s\n", temp);
//close file, return 0 for main
fclose(file);
return(0);
}
any help would be appreciated
The reason your program can't get the file name is because you're not giving it to it.
If you run your program as:
prog hello.txt
it's given the argument hello.txt in argc/argv.
However, what you're doing is:
cat hello.txt | prog
which means the shell is opening the file and feeding it into the standard input of your program. Actually, to be more accurate, cat is opening the file and the shell is simply connecting the standard output of cat to the standard input of prog.
One way around this is to check the number of arguments (argc is usually the count, argv[] the values, despite the roundabout way you have it in your code) and, if it's zero, argc == 1, read your file from standard input.
Only if an argument is given do you open that file and read it. That's the way a lot of UNIX utilities work:
od -xcb hello.txt # will dump the file.
cat hello.txt | od -xcb # will dump the file, but using standard input.
echo hello | od -xcb # will dump your "hello" string.
Some even change their behaviour depending on how they're invoked, wc being one example - it shows the file name(s) if it knows them:
pax> wc -l qq.c
29 qq.c
pax> cat qq.c | wc -l
29
pax> wc -l *.c
0 a b.c
168 binmath.c
49 qq-save.c
29 qq.c
11 qq2.c
5 qqq.c
18 xx.c
280 total
pax> cat *.c | wc -l
280
Note that last case - because all files are being presented over the single standard input stream, there's no way to tell how many files there are. wc will just tally up the entire contents of that stream.
Try this instead:
#include <stdio.h>
#include <string.h>
#define DEFAULT_LEN 70
int main (int argc, char *argv[]) {
FILE *file;
// Either select standard input or open the given file.
if (argc == 1) {
file = stdin;
} else {
file = fopen (argv[1], "r");
if (file == NULL) {
printf ("I'm sorry Dave, I can't do that\n");
printf (" (open the file '%s', that is).\n", argv[1]);
return 1;
}
}
// Now you're connected to stdin or the file itself, no
// difference in handling them (unless you do weird fseek
// sort of stuff).
char temp[DEFAULT_LEN];
fgets (temp, DEFAULT_LEN, file);
printf ("%s\n", temp);
// Only close file if you opened it yourself.
if (argc != 1)
fclose (file);
return 0;
}
This allows you to use both the file and standard input method as follows:
pax> prog prog.c
#include <stdio.h>
pax> echo hello | prog
hello
Piping content into a process does not put the values into argv; rather, it puts the values onto that process's stdin.
You need to check if argc is greater than 1. If it is, then argv[1] has the filename you've been given (well, the first argument to the program, anyway). If not, you need to read from stdin to get the filename.
To read from the contantenated, you need to read from STDIN
char c = (char)fgetc(stdin)

Resources