Redirection ambiguity [duplicate] - c

This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Closed 4 years ago.
I have a file named test.c (Its contents are given below)
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("T\n");
fork();
printf("F\n");
}
The expected result is:
T
F
F
Compiling with gcc and executing ./a.out ,the output is:
T
F
F
which matches the expected answer.
But ambiguity arises when I redirect the output to another file.
$ ./a.out > Output.txt
The Output.txt has the following data:
T
F
T
F
Why is there an additional T in my Output.txt file when I use redirectors?
1) I've check this in multiple PC's running on ubuntu with gcc installed.
2) Tried deleting the Output.txt and moving all the files to a different location, but this still persists.
P.s. this works fine without the redirector.

I think it's because of the buffer, give a try with the follow code:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("T\n");
fflush(stdout);
fork();
printf("F\n");
}

Related

Printf() not executing before the "while(1)" loop in C language in Linux [duplicate]

This question already has answers here:
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 5 years ago.
I noticed something strange with the following code.
int main()
{
printf("Test"); // Section 1 do something here....
while(1)
{
;
}
}
Section 1 should be executed first, then the program should get stuck in while loop.
But the result was that "Test" didn't get printed, but it got stuck in the while loop. I wonder why the code in Section 1 does not get executed?
I ran the code on Ubuntu 14.04 LTS(compiled with the default gcc compiler)
The stdout stream is buffered, therefore it will only display what's in the buffer after it reaches a newline. Add :
fflush(stdout);
after line :
printf("Test");
See also this link for other alternatives.
This must work:
#include <stdio.h>
int main() {
printf("Test");
while(1){}
}
To compile:
gcc file.c
To execute:
./a.out

gcc: fatal error: regex.h: No such file or directory [duplicate]

This question already has an answer here:
Regex.h for windows
(1 answer)
Closed 4 years ago.
For my C programs, I am using gcc.
Ultimately, I want to write a program that does stuff with regular expressions. Right now, however, my program simply outputs Hello World:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <regex.h>
int main(void) {
printf("Hello World\n", regex);
return 0;
}
Here's how I compiled that program:
gcc -std=c99 -o helloWorld helloWorld.c
That produces this error:
fatal error: regex.h: No such file or directory
I'm a C newbie. Any help would be much appreciated.
See what happens if you drop .h from regex.h

Cygwin showing error iostream.h is unable to locate [duplicate]

This question already has answers here:
fatal error: iostream.h no such file or directory [duplicate]
(3 answers)
Closed 8 years ago.
I wanted to run C programs on windows in order to achieve this I downloaded cygwin(Linux like environment for windows) made a program and kept it on a directory called ..\cygwin\home\Computer
Code goes here
#include<iostream.h>
void main(){
printf("Hai");
}
When i am trying to execute this program using command prompt.
$ g++ hai.c
Its throwing out an error
hai.c:1:21: fatal error: iostream.h: No such file or directory
#include<iostream.h>
^
compilation terminated.
What is going on any idea?
change to
#include <iostream>
#include <cstdio>
int main(){
printf("Hai");
}
or with g++ -x c hai.c or gcc hai.c
#include <stdio.h>
int main(){
printf("Hai");
}

C program sqrt not working [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I also entered include<math.h> but it still doesnt work. People are saying to enter -Im but im new to this where do I put -Im and how do I fix this.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
float a=0, b=0, c=0, root1=0, root2=0;
printf("Enter the value of a,b and c to determine the roots\n");
scanf("%f%f%f",&a,&b,&c);
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("The first roots of the quadratic equation are\nFirst root=%.1f\nSecond root=%.1f",root1,root2);
return 0;
}
You have a copy-paste bug here:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
should be:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root2=(-b-sqrt(b*b-4*a*c))/(2*a);
Also you may need to link with the math library, e.g.
$ gcc -Wall foo.c -o foo -lm
Two things: first you copy pasted "root1" twice so you will lose the "plus" value and root2 will be zero. Second, for the benefits of others, the problem is most probably at compile time and the googled answer is there:
http://www.cs.cf.ac.uk/Dave/C/node17.html
And you should test for imaginary values:
if(b*b-4*a*c < 0){
printf("error: complex solution unsupported, see http://en.wikipedia.org/wiki/Square_root\n");
exit(1);
}

Can an executable discover its own path? (Linux) [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how to find the location of the executable in C
I would like an executable to be able to discover its own path; I have a feeling that the answer is "you can't do this", but I would like this to be confirmed!
I don't think I can use getcwd(), because I might not be executing it from the same directory. I don't think I can use argv[0], because that is based on the string that's used to execute it. Are there any other options?
Rationale
The real problem is that I'd like to place an executable somewhere on a filesystem, and place a default config file alongside it. I want the executable to be able to read its config file at runtime, but I don't want to hardcode this location into the executable, nor do I want the user to have to set environment variables. If there's a better solution to this situation, I'm all ears...
The file /proc/self/exe is a simlink to the currently running executable.
Edit: It was pointed out that using /proc/self/exe is more straightforward. That is entirely true, but I didn't see any benefit in editing the code. Since I still get comments about it, I've edited it.
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
int main()
{
char dest[PATH_MAX];
memset(dest,0,sizeof(dest)); // readlink does not null terminate!
if (readlink("/proc/self/exe", dest, PATH_MAX) == -1) {
perror("readlink");
} else {
printf("%s\n", dest);
}
return 0;
}
Initial answer:
You can use getpid() to find the pid of the current process, then read /proc/<pid>/cmdline (for a human reader) or /proc/<pid>/exe which is a symlink to the actual program. Then, using readlink(), you can find the full path of the program.
Here is an implementation in C:
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
int main()
{
char path[PATH_MAX];
char dest[PATH_MAX];
memset(dest,0,sizeof(dest)); // readlink does not null terminate!
pid_t pid = getpid();
sprintf(path, "/proc/%d/exe", pid);
if (readlink(path, dest, PATH_MAX) == -1) {
perror("readlink");
} else {
printf("%s\n", dest);
}
return 0;
}
If you want to try, you can then compile this, make a symlink from the executable to an other path, and call the link:
$ gcc -o mybin source.c
$ ln -s ./mybin /tmp/otherplace
$ /tmp/otherplace
/home/fser/mybin
Use the proc filesystem
Your flow would be:
Get pid of executable
look at /proc/PID/exe for a symlink
Well, you have to use getcwd() in conjuction with argv[0]. The first one gives you the working directory, the second one gives you the relative location of the binary from the working directory (or an absolute path).
Get your name from argv[0] then call out to the which command. This will obv only work if your executable is in $PATH.

Resources