This question already has answers here:
Re-opening stdout and stdin file descriptors after closing them
(3 answers)
Closed 6 years ago.
I have one question,studying standard I/O.
#include <stdio.h>
int main(){
void *mem = malloc(0x80);
close(0)
/*
something code (solution)
*/
read(0,mem,0x80);
}
with above code, Is possible opening stdin ?
before ask question on stackoverflow, i thought that open('/dev/tty'); .
but it not seem to completely open stdin.
i hope that do not use dup()
In my view, the ideal way to achieve this would be
use dup() to create a backup fd.
close() the stdin.
perform the operations
restore stdin using the dup2() and passing the previous backup fd.
Related
This question already has answers here:
Strange behavior performing library functions on STDOUT and STDIN's file descriptors
(2 answers)
Closed 5 years ago.
I'm having difficulity understanding the read function in C.
len = read(fd, buf, 32);
when I assign fd as 0,1,2 and run the program, its basically doing the same thing, can someone tell me what difference does this make?
read() attempts to read up to count bytes from file descriptor fd.
fd = 0
fd = 1
fd = 2
Is reading from different file descriptors. The difference is, you are reading from different files, and the data read into the buffer is different.
What is the difference in reading from Book A and reading from Book B ? it is the same process of reading a book... it is the content that changes.
As far as I understand your question it is why nothing changes if you read from file descriptors 0, 1, 2.
In a normal program the file descriptor 0 is stdin, 1 is stdout and 2 is stderr. stdin is where you should read your input, 1 is where you should write your output and 2 is where you should write your error messages.
It is not uncommon that all three file descriptors may point to the same underlying file (a file can also be the console, network connection, etc.) behind the scenes. If you're just running your program from the command line this is actually quite likely. In that case you may be able to read from all of them and get the exact same result.
But. Then you decide that you want to save the output of the program in a file and run it like this: program > output. Now file descriptor 1 is no longer pointing to the same file as stdin and your program would break. Same thing happens if you point stderr to some error logging facility. Or get the input from a file or a pipe. Or run the program in some debuggers. Or a different terminal. This is why you should only read from 0 and no other file descriptors, even if you might get away with it sometimes.
This question already has answers here:
Difference between fclose and close
(4 answers)
Closed 6 years ago.
I want to redirect STDOUT to a file on the disk. The point is to make the printf on my program write to a file instead of the console.
I saw some articles on the web where they used:
dup2(fileno(outputFile), STDOUT_FILENO);
Or:
close(STDOUT_FILENO);
dup(fileno(outputFile));
In every tutorial they use close() and it actually works. But I was curious and I tried to use fclose(stdout) instead but some error happened when I tried to use printf:
fclose(STDOUT_FILENO);
dup(fileno(outputFile));
Error:
Bad file descriptor
My question is, why does fclose() not work but close() does?
Thanks.
STDOUT_FILENO is a numeric file descriptor (usually 1). When you use close, you release the descriptor, but then reassign it with dup2. Any output to that descriptor will now go to the new file.
stdout on the other hand is a FILE*, an object of sorts that contains a file descriptor. Doing printf formats output into a buffer associated with the FILE, and then writes the buffer to the descriptor (depending upon the buffering mode). When you fclose a FILE*, it (normally) closes the underlying descriptor, and frees the FILE object. The dup2 does not ressurect stdout, so printf fails.
This question already has answers here:
C fopen vs open
(11 answers)
Closed 8 years ago.
There are differences between open() and fopen() function. One is system call and other is library function. I try to figure out what is the application of these two function but I found nothing useful. Can you give some scenarios where to use open() and where fopen() should be used?
Sometimes you need a file descriptor. open() gives you one. Sometimes you need a FILE*, in which case use fopen(). You can always turn your FILE* into a file descriptor via fileno(), whereas the opposite transformation is not really supported. It mostly depends on what downstream functions you intend to call with the file handle.
open() will return the file descriptor. We overwrite the file, while using the fopen() we cannot overwrite the file. We use the file descriptor for reading and writing using the other
functions like read() write() etc. But in fopen() it will return file descriptor we have to
use fprintf() to write to the file stream. sscanf() to read from the stream.
This question already has answers here:
Interprocess communication using pipe in Linux
(2 answers)
Program based on pipe in Linux
(2 answers)
Closed 8 years ago.
Does any one know how to write a number to pipe for that first of all i have to open the pipe, and then write suitable number to it.
Also,after writing also i have to read the number and print it.
I have tried to open the pipe with 'popen' command but i am uncertain what to give as the 1st argument to popen i.e:-
popen(const char *command, const char *type)
i want to ask what command should i give here if i want to write a number to pipe..??
you should use fd=open(FIFO_NAME,O_RDONLY); for reading fifo file.
mkfifo(FIFO_NAME,S_IFIFO|S_IRWXU|S_IRWXG|S_IRWXO);
fd=open(FIFO_NAME,O_WRONLY);
fifo is also called pipe.Here mkfifo create a fifo file and the write into a file.
Note:- that to read or write into fifo there should be a reader and writer process, Else it would be blocking one of the process.
if you don't want to write a process for reading the use cat command and writing process to see the fifo file content.
This question already has answers here:
Prevent file descriptors inheritance during Linux fork
(3 answers)
Closed 3 years ago.
Is it possible to mark a specific file descriptor as not inheritable, or close it, in the child process when fork() is invoked?
No. All file descriptors are inherited in fork. You can set a fd to be closed on exec, however, by using fcntl(fd, F_SETFD, FD_CLOEXEC).
No its not possible. By default child processes with inherit file table from parent process.
If you really want close-on-fork, something like this could work:
static void fd_to_close;
static void closer()
{
close(fd_to_close);
}
pthread_atfork(0, 0, closer);
Normally close-on-exec is the desired behavior anyway, though.