Redirecting standard output in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so continuing with the same program that we worked on last video which again you can check up top what I want is well right now if I try to launch the program you know we are getting the output here on the terminal right which is okay I guess because we have both this success and this some post-processing goes here message that we are sort of using but I don't really want this message from ping I just want to know if we were able to ping to that website it worked is it not that's all I want to know I don't want to know the specifics how can I tell my here my pink program to stop printing on the terminal can we do that in Linux yes we can actually do that let's say we want to just output all the data into another file right so everything that is printed here on the terminal I want just to be in a file separate of everything else how do we do that well first let's create a file right let's open and create a file we can do so easily using the open function so open and this guy is actually found in F cntl handles so f cntl header at h and if i call here open is just the path to the file as we have used before with FIFO so here we just say let's call it pink to results dot txt that's fine and I want all flag to be all underscore right only but I also wanted to create the file if it doesn't exist there'll be I think that'd be a nice feature to have how can we do that using the open function well this guy you can see this is a flag and because there are flags you can actually add more to them more options to them if I type in here o underscore and actually scroll back up you'll see there are a few quite a few fun Flags that I can open the file waves and there's like app and sync and then there's our if you remember the readwrite or just read-only what I want is to just open it and try to create it so there is all underscore create I'm not sure why they eat the e at the end but that's fine so this is what the flag the flag is called and in order to specify multiple Flags like in many other systems what you have to do is just have a bitwise or in between them and since this one e has the value 0 1 is just 1 in octal apparently and this one is 0 100 in octal well they are different and once you if you bitwise ordem they're gonna be both them are gonna be found in there and the open function is gonna know that we need to create this file with these parameters right and last we need to specify with what what are the permissions for the file so remember we had to specify with make FIFO the permission of the file similarly here we have to do that I'm gonna just specify that everybody has access to it so I'm just typing in 777 in octal again and don't forget this zero is very it is very important Stelling see that it has to this is an octal value not just a decimal value again if I hover over it my ID does transform this to a decimal value which is 511 so okay this is very important and this open call will well try to open the file if it doesn't exist it will create it and with those permissions everybody could read and write with no problem and it will open it and we could start writing into it from here onwards I'm going to check as well if the file is negative one because if it is negative one again some error code so I can just say let's just put some two or something here doesn't really matter at this point and now how do we when we have the file but how do we say okay well we want the output of this ping program to go to to go to our file ping results how do we do that now first I want to show you a bit of an overview as to how the file descriptor and files situation easy in Linux so here suppose I'm talking about the child process and this is nothing too sketchy we don't we don't take a look at multiple multiple processes at the same time just the child process and what our file descriptors right we we have been using them quite a few times here if I take a look at the code this file here is actually a file descriptor if you hover if you take a look at the documentation of open it says that it does return the file descriptor make FIFO does return the file descriptor when you make a pipe those are also file descriptors now finally scaler what it is is just in number that is unique across the process right so two processes could have the same file descriptor with number three but the same process cannot have two file descriptors with both both being the number fitness is maintained by the operating system and these really represent a key or a handle to an input or output resource that could be a file that could be the terminal input the terminal output or standard output standard input the standard error output or it could be a pipe and so on and so forth okay there are just things that you can read or write to and whenever you open a process automatically Linux starts and opens certain file descriptors for you so automatically the file descriptor with IDs zero is actually linked and open to handle STD in solo standard input so zero is linked to the standard input that means that well if you would actually call a read on 0 that would be similar to saying scanf although no formatting just basically read the bytes you're getting the file descriptor with ID 1 is going to be the standard output so if you're right - if for example do write of 1 and you actually write some stuff here from a buffer you we'll output on the terminal and the file descriptor with ID - is the standard error so it's the error this is where all the errors go to so if you print out a message this represents an error this is where it goes - usually when you're launched a program is just going to go to the terminal so similarly twice to the out but is going to shop in red or something like that but they are separate so you can actually filter them out if you want so these free file descriptors actually opened by default before you even get to the first line in main they are opened okay so we have STD in a studio 10 STD error and whenever you actually open a file like we did in the in here so we said in file equals open of all these this file got a file descriptor I was in this list and was added here at the end probably with ID 3 right and well it it does point to our pink results dot txt okay so that let's find that working right now but what we want though to have a pink program actually output whatever it outputs to the terminal to this pink results file what I have to do a bit of a trick here and that is we want to actually replace this this one here's for that instead of pointing to a CD out pin appoints two pink results how do we do that well in Linux there's a dupe function so there are many actually variants of this tube function doesn't loop and loop - first we can take all that dupe the text in just a single planet or just a single file descriptor so here we can give it the file so this guide is tube what it does is return another file descriptor for our ping results so let's call this file to this doop function it took in our file descriptor our free here in and duplicated it made another one and it returned another file descriptor with the ID probably four it might be another number if you have already opened some other files but it's probably four and again it just points to the same exact file here so that's nice you can actually have two file descriptors that point to the same file and completely read or write from it without any issues okay but what's the using that well dupe by itself doesn't do anything but there's another function called Duke - that takes in two parameters one is the the file descriptor if we want to actually clone or duplicate but the second one is the value that we want the new file descriptor to actually have well if we say dupe to file and we actually type in here one well we're actually going to get file two equals one but this what this is going to do is instead of creating another value here is going to take a look at one and say take a look at the file description is gonna find that one is indeed it's here and first it's actually going to close this stream she's going to close STD out completely we're no longer gonna have it so I can just delete it and then it's going to open it again to our ping results because remember we're duplicating it so it should be the same as this guy with this new ID with these new file descriptors and I say here ping results dot txt and clearly this is what we want we just duplicated this file descriptor we have opened before into STD out and for all this to work remember exactly I said it does overwrite everything the code the memory but it doesn't change the process ID nor does it change the file descriptors that are already opened so those remain that means that means that when we change the STD out here right before executing the ping program it's going to change it inside the pink program when the pink program says print F something something this printf is going to try to print to STD out but STD how it is actually going to be a file and it's going to print to that file instead of printing out in the terminal okay so this is how all this works we have opened the file we got a new file descriptor with ID probably three but then we duplicated it we duplicated this guide is free into being a file descriptor with the value 1 which is the STD out so we can change this instead of saying 1 we can change this to basically a constant STD out underscore file number and as you can see this is still just 1 but it is better to just use it because it is in the c standard and it's most likely going to be 1 but on some who knows on some machines it might not actually be one but you know that that's the standard output and before we finish but this is almost finished the only issue is that remember we have two file descriptors they are both open to ping results that's not really a problem but we're going to use one of them we use this one to well as STD out for it to print into the file whatever it means to try to pin to STD out but this guy is not used so we should just close it this guy is no longer used even though it got duplicated it's fine we enclose this and still have this open so what we can say here is a close of file right our file our original ID which was probably free and this file - we don't really need it because we know it is one right so we can simply remove it but just for the sake of completeness I'm gonna actually print F [Music] the duplicated FD and percent the backstitch and file2 and I'm going to also print out in lessons here the ofd to ping results of send the file just file and if I try to launch this now you'll notice something very interesting so as you can see empty two ping results is free but we didn't get anything else we didn't get this message well we did not get this message because we have already swapped the STD out right so here the STD out was swapped and we print f2 STD out but STD out was ping results so we should see the result in that file called ping Rizal so text which we have here if we try to open it you indeed see that the duplicated file descriptor is one right so this is what this guy printed but in fact we don't need this because we know it's one we can just remove this and we can also remove this printf and now if we just try to run this every single time we run this you'll notice in the terminal we don't get anything on the screen from ping but inside these ping results we actually get the statistics and everything 64 bytes google.com and everything it works very nice now there was a lot in just one single video if you didn't understand parts of it parts with regarding the open function or regarding the Dib - or regarding file descriptors that we have talked about be sure to let me know I might actually create another section in the whole course or just create our course for these specific functions because there's quite a lot to the emergency I just kind of scratched the surface on them but I think this was very important to learn when it comes to executing programs from within your programs inside C and basically changing the output of programs that are executing I think that was important for you to learn if you do have any questions leave them down comments below or on our discord server I make sure to answer every single one of them thank you so much for watching take care bye you
Info
Channel: CodeVault
Views: 16,189
Rating: 5 out of 5
Keywords:
Id: 5fnVr-zH-SE
Channel Id: undefined
Length: 15min 38sec (938 seconds)
Published: Sun May 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.