Home > OS >  Standard output is redirected to the files and the printf function
Standard output is redirected to the files and the printf function

Time:11-11

1. Use dup2 close under the Linux standard output, use printf print information,
2. The code is as follows:
#include
#include
#include
#include

Int main ()
{
Int fd1=open (" eup. TXT ", O_RDWR | O_CREAT | O_TRUNC, 0644);
If (fd1==1) {
Printf (" # LINE % d: "__LINE__);
Perror (" open ");
return -1;
}
//printf (" Hello, World! \n");//do not add this sentence, eup. No content in TXT,
Int fd2=dup2 (fd1, 1);
If (fd2==1) {
Printf (" # LINE % d: "__LINE__);
Perror (" open ");
return -1;
}
Printf (" \ \ nfd2 fd1: % d: % d nfd3: % d \ n ", fd1 and fd2, 3);
Close (fd2);
Close (fd1);
return 0;
}
3. The problem is: printf (" Hello, World! \n"); Comment out this sentence, to run the program after eup. TXT inside
There is no any content, this is correct, but after uncomment, run the program, found the eup. TXT inside
Is the following:
Fd1:4
Fd2:1
Fd3:3
Why is this? Which function to refresh the file buffer??
First \ n cannot refresh file buffer, if is the close refresh, why annotation printf (" Hello, World! \n");
This sentence after running the program without the content refresh to eup. TXT file??
What a great god know??


CodePudding user response:

In cplusplus forum should also is you, don't know where do you see this problem, a bit much,
Because of this problem, learning some function, thanks!
Close () can not refresh buffer, there are clear, in this the man close and also use fsync mentioned; But the fsync to tty is invalid, so also not line,
reference
A successful close does not guarantee that the data has had successfully saved to
Disk, as the kernel defers writes. It is not common for a filesystem to flush the
Buffers when the stream is closed. If you need to be sure that the data is physically
Stored, use fsync (2).

After trying, there are three ways to write eup. TXT,
 # include & lt; Stdio. H> 
#include
#include
#include

# define METHOD 1

Int main ()
{
# if METHOD==1
Setvbuf (_IONBF stdout, NULL, 0);
# endif
Int fd1=open (" eup. TXT ", O_RDWR | O_CREAT | O_TRUNC, 0644);
If (fd1==1) {
Printf (" # LINE % d: "__LINE__);
Perror (" open ");
return -1;
}
/* printf (" Hello, World! \n"); */
Int fd2=dup2 (fd1, 1);
If (fd2==1) {
Printf (" # LINE % d: "__LINE__);
Perror (" open ");
return -1;
}
Printf (" \ \ nfd2 fd1: % d: % d nfd3: % d \ n ", fd1 and fd2, 3);

# if METHOD==2
Char buf [200]={0};
Sprintf (buf, "\ nfd2 fd1: % d: % d \ nfd3: % d \ n", fd1 and fd2, 3);
Write (fileno (stdout), buf, strlen (buf) + 1);
# endif

# if METHOD==3
The fclose (stdout);
# endif
Close (fd2);
Close (fd1);
return 0;
}

1. Change the stdout to no buffer, and buffer mode have a lot to do (?)
2. Use the write
3. Close (fd2) - & gt; Why fclose (stdout), fclose magical, look at the glibc/libio/iofclose _IO_new_fclose c source, said don't understand...

In conclusion, the problem with Linux/Unix system kernel may be more relevant; Commonly used the standard C library functions,

CodePudding user response:

I asked, I just want to know without annotation printf (" Hello, World! \n"); Under the premise of running the program, why printf (" \ nfd2 fd1: % d: % d \ nfd3: % d \ n ", fd1 and fd2, 3); Can put the content into eup. TXT,

Later saw some information said: printf to the terminal or line is the output of the screen buffer (\ n can brush buffer), file output is the whole buffer (buffer full printing, general buffer can not filled with a few characters),

Comment out the printf (" Hello, World! \n"); Later, due to the int fd2=dup2 (fd1, 1); That standard output to file, printf is full buffer, so the printf (" \ nfd2 fd1: % d: % d \ nfd3: % d \ n ", fd1 and fd2, 3); \ n do not brush in the buffer, the data in the buffer, just didn't write to the file,

Don't comment printf (" Hello, World! \n"); Behind, even call the int fd2=dup2 (fd1, 1); Printf or line buffer, not into the whole buffer (cause I don't know, just listen to other people say), so the printf (" \ nfd2 fd1: % d: % d \ nfd3: % d \ n ", fd1 and fd2, 3); The \ n can brush in the buffer, data is written to the file,

CodePudding user response:

Good! I found this article content follow your train of thought - & gt; To stackoverflow
You should also can see understand, I still say I understand it,

Whether the C/C + +, its input and output function will not be responsible for the line - buffered or full - buffered, this is decided by the output file attributes to;
If it is interactive device, then the line - buffered;
Otherwise, it is full - buffered,

But the C/C + + how to judge of the output file attributes? The paper said a different implementation, though different, but almost all use the stat ();
And once decided to a way of caching, behind won't change (unless explicitly setvbuf ())

So, first printf, C, have made it clear that its output to stdout guide terminal tty, so use the line - buffered way,
But then use dup2 () redirection, but it is not know for C, still use the line - buffered way to refresh it buffer,

However stdout redirection to the file at the beginning, C can be aware that the information and change the way into a full - buffered,
  • Related