# include & lt; An FCNTL. H>
# include & lt; Unistd. H>
# include & lt; String. H>
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
Int main () {
Int fd.
Int err_no;
The FILE * fpout;
Char buffer [256].
Char buf [2048].
Char * buf1;
Char * CMD="cat/proc/meminfo";
Buf1="This is testing for fputs... \ n ";
int i=0;
for(i=0; I & lt; 29. I++) {
Buf [I]=buf1 [I];
}
Printf (" CMD is: % s \ n ", CMD);
Fpout=fmemopen (buf, 2048, "a +");
Fd=fileno (fpout);
Printf (" fpout pointer value is: % d \ n ", fpout);
Err_no=fprintf (fpout, "This is testing for fputs... \n");
Printf (" fprintf the return value is: % d \ n ", err_no);
Fseek (fpout, 0, SEEK_SET);
The fgets (buffer, 255, fpout);
Printf (" get string is: % s \ n ", buffer);
Memset (buffer, 0, 256);
Printf (" fd no. Is: % d \ n ", fd);
Printf (" stdout no. Is: % d \ n ", fileno (stdout));
Dup2 (fd, 1);
Printf (" after dup2, fd no. Is: % d \ n ", fd);
Printf (" after dup2, stdout no. Is: % d \ n ", fileno (stdout));
System (CMD);
While (the fgets (buffer, sizeof (buffer), fpout)) {
Printf (" % s ", buffer);
}
The fclose (fpout);
return 0;
}
CodePudding user response:
Fileno () cannot obtain fmemopen () memory FILE descriptors of streaming files, because fmemopen () memory no FILE descriptors, streaming files fmemopen () to initialize the FILE structure can be written in the default - 1,CodePudding user response:
But this seems to say don't in the past, a lot of things to file under Linux programming mode operation, basic it is a file descriptor, including stdin, stdout, stderr, a stream is a memory, their default descriptor is 0, temporarily can't have found online sources said fmemopen () to create a stream of memory no file descriptors, wonder,CodePudding user response:
Do you want to! FILE encapsulation structure is on the FILE descriptor, there is a field to store the FILE descriptor, is the most important is the FILE structure in application layer, and the FILE descriptor is the kernel layer data operation, fmemopen () open the memory stream FILE pointer, in fact all of the data are in the application layer of memory, it is necessary to use the FILE descriptor from the kernel layer to control the application layer of memory? So directly to fill 1,And fmemopen is application layer function, it should be no corresponding system call, there is no system calls and the kernel doesn't matter much, of course, there is no file descriptors
CodePudding user response:
Yeah, well, is only a temporary don't have it, because the original code is to intercept execvp () call command is screen printing, using pipe () to build a pipeline, the fork () the child process, then the pipeline to write the file descriptor dup2 mapped to the child to the standard output file descriptor table stdout, then the parent read file descriptor read data from the pipe, but I think such a fork to fork to still use the pipeline is more troublesome, why isn't the current process to build a memory stream file, put it in the descriptor dup2 stdout, and then I direct system () call command can intercept the screen output, failed, I wonder if pipe () to create the pipe is also a memory stream in the form of a file, that why it is the file descriptor, it is not clear pipe () do apply to the kernel in the file descriptor, free again study,