Redirecting standard output in C

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 พ.ค. 2020
  • Check out our Discord server: / discord

ความคิดเห็น • 142

  • @XAyaDubX
    @XAyaDubX ปีที่แล้ว +57

    This may be the last video I watch of yours at least for a while as my semester is ending and I most likely will never write in C again. I thank you for helping me through my semester. You were a comforting and friendly face through the whole thing. You helped me on a lot of my assignments. You helped me understand programming much better as a whole. I will always be thankful.

  • @DaveTheLesser
    @DaveTheLesser 3 ปีที่แล้ว +68

    I honestly can't believe this channel is so small because these videos are super informative and have been floating me through my systems basic class. Thank you for the great explanations!

  • @13012IS
    @13012IS 3 ปีที่แล้ว +17

    Not all heroes wear capes. Thank you very much for these lessons

  • @aleksaleks463
    @aleksaleks463 14 วันที่ผ่านมา

    these are really the best videos on C out there. thank you so much!

  • @lylesnodgrass1961
    @lylesnodgrass1961 2 ปีที่แล้ว +1

    These are wonderful presentations. They're relevant, accurate, clear, succinct, and nicely done. Thank you.

  • @sm8739
    @sm8739 2 ปีที่แล้ว +4

    You channel is 100% underrated, neatly organized and perfectly explained!

  • @darthvader4899
    @darthvader4899 2 ปีที่แล้ว +1

    Thank you so much for such a great video. Keep it up and I hope people will realize how fantastic this channel is.

  • @LamNguyen-jp5vh
    @LamNguyen-jp5vh ปีที่แล้ว +1

    Thank you so much for releasing this video! It helped me a lots in OS classes!!!

  • @RedXmysterious
    @RedXmysterious 3 ปีที่แล้ว +23

    This is a very good explanation!! Definitely saved me for my assignment

  • @christoffer1125
    @christoffer1125 3 ปีที่แล้ว +1

    Your videos are legitimately amazing.

  • @reverse9822
    @reverse9822 ปีที่แล้ว +2

    I appreciate your explanation and energy, all my peers watch your videos and helps us finish our projects , thank you so much.

  • @Jason-lb1lu
    @Jason-lb1lu 3 ปีที่แล้ว +28

    this is awesome, so hard to find help on this online. Other sources either have no example or an example too complicated.

  • @abulkalam7729
    @abulkalam7729 2 ปีที่แล้ว +1

    Your content is just amazing, easy to understand.. really helpful, for sure deserve more viewers/subscriber,
    Thanks again...really appreciate it..

  • @hectordoyle4718
    @hectordoyle4718 4 ปีที่แล้ว +2

    woow
    you are amazing - thanks for the requested video!
    great explanation!

  • @bama2619
    @bama2619 3 ปีที่แล้ว +4

    Rewatched pausing video sometimes, making notes. At last got it. Thank you. Great explanation. You have a talent for explanation. Great job!

  • @alexbianchi72
    @alexbianchi72 2 ปีที่แล้ว

    This is the most helpful video I have watched in my entire life. I am not even joking. Thank you so much!!

  • @AbdulRehman-lt2gm
    @AbdulRehman-lt2gm 3 ปีที่แล้ว +1

    Your videos are so informative better then my lecture in the class. it really helpful. Thank you for the this helpful content

  • @souhaylsoulami7591
    @souhaylsoulami7591 3 ปีที่แล้ว +2

    best teacher ever, I bingwatch you. Thank you very much for sharing!!!

  • @oliviern.2095
    @oliviern.2095 2 ปีที่แล้ว

    Great explanation, incredible. Thank you so much.

  • @marcosstrasorier7
    @marcosstrasorier7 ปีที่แล้ว

    thank you so much for this serie of videos! are very important for me and i used to study for my university! I am from argentina and i'm continue learning more trough your explanaitions (flawless, indeed)!!!

  • @t2a392
    @t2a392 ปีที่แล้ว

    Helped me to undestand dup2. Love ur content, hope ur channel will grow bigger, u deserve it.

  • @nivaspvs7220
    @nivaspvs7220 3 ปีที่แล้ว

    Thank you so much for sharing your knowledge .
    2021 first best thing is finding this channel.....

  • @eyaltb
    @eyaltb 3 ปีที่แล้ว +10

    Man you are absolutely amazing. You explain so simply what my professor and 2 TA's can't even give me basic intuition for.
    THANK YOU

    • @wahidredjradj1838
      @wahidredjradj1838 3 ปีที่แล้ว

      +1 Absolutely

    • @danielsette2201
      @danielsette2201 2 ปีที่แล้ว

      I feel like you guy. I'm listening to this video and understanding in a language that i'm not that knowledge...Thanks a lot!!!

  • @gracemcpadden3708
    @gracemcpadden3708 3 หลายเดือนก่อน

    You are amazing! Thank you!

  • @user-lv7ff7rl8y
    @user-lv7ff7rl8y ปีที่แล้ว

    Man, u are cool. Ty very much. My lecturer did not explain the material, but with your help, I learned everything on my own!!

  • @dtantarjent144
    @dtantarjent144 2 ปีที่แล้ว

    Perfectly explained, great work!

  • @CodeWithEz
    @CodeWithEz 2 ปีที่แล้ว

    I'm blown away by this video, thanks for showing these hidden tricks.

  • @puissant218
    @puissant218 2 ปีที่แล้ว

    Helped me a lot, thank you !

  • @noahrodriguez1895
    @noahrodriguez1895 3 ปีที่แล้ว +1

    smart teacher. great comprehension of the material and great explanation of the material

  • @sethsarchet1020
    @sethsarchet1020 2 ปีที่แล้ว

    Great video!! This explanation will really help me on a project I'm working on

  • @quocatle7140
    @quocatle7140 3 ปีที่แล้ว

    this is an awesome video, thank you very much !!

  • @rohailasad249
    @rohailasad249 3 ปีที่แล้ว

    Thanks for the video. It helped a lot

  • @bahaeddineabdellawi1551
    @bahaeddineabdellawi1551 3 ปีที่แล้ว

    thank you for this amazing video

  • @juggles5474
    @juggles5474 ปีที่แล้ว

    incredible video, thank you!!

  • @anstbt
    @anstbt 2 ปีที่แล้ว

    life saver sir , thank you

  • @multivalfran
    @multivalfran ปีที่แล้ว

    AMAZING. BEST CHANNEL EVER

  • @hylorial
    @hylorial 2 ปีที่แล้ว

    YOU ARE THE MVP

  • @santiagolaseul
    @santiagolaseul ปีที่แล้ว

    you are amazing.

  • @theodeville8222
    @theodeville8222 2 ปีที่แล้ว

    you are the big boss ty

  • @nofaith5410
    @nofaith5410 3 ปีที่แล้ว +1

    I love you man you just saved me

  • @phyroxdimebag3862
    @phyroxdimebag3862 2 ปีที่แล้ว

    awesome video!

  • @oussamazz09
    @oussamazz09 3 ปีที่แล้ว

    thank you so much you for your efforts...

  • @nom9d825
    @nom9d825 2 ปีที่แล้ว +1

    omg)) dup2 is so doooope. i expected some trick with redirect, but this.. thanks a lot!

  • @hamzabayar3547
    @hamzabayar3547 5 หลายเดือนก่อน

    it was helpful. Thanks a lot

  • @abhimanyumithun832
    @abhimanyumithun832 10 หลายเดือนก่อน

    videos are super informative , wannted more video on pipes and dup2 on realtime application .
    Thank you for the great explanations

  • @mahirkayadelen9202
    @mahirkayadelen9202 ปีที่แล้ว

    Good explanation thanks

  • @zinedineaitaider5229
    @zinedineaitaider5229 3 ปีที่แล้ว

    very good explanation thank you

  • @ahmetkemalbayraktar7017
    @ahmetkemalbayraktar7017 2 ปีที่แล้ว

    thank you very much

  • @stormz4040
    @stormz4040 3 ปีที่แล้ว

    Thank you for the video

  • @wenlix8690
    @wenlix8690 3 ปีที่แล้ว

    ty very much you're the best (again)

  • @kar3lio
    @kar3lio 2 ปีที่แล้ว

    Muy buenos videos, me han ayudado mucho 👍

  • @kritonios
    @kritonios 8 หลายเดือนก่อน

    Very nice video

  • @isikozsoy
    @isikozsoy 3 ปีที่แล้ว

    Thanks a lot!

  • @stevendam8031
    @stevendam8031 3 ปีที่แล้ว

    thank you mate

  • @techgnosis3693
    @techgnosis3693 3 ปีที่แล้ว

    Good stuff broo...

  • @lionbursh6342
    @lionbursh6342 3 ปีที่แล้ว

    Thank you!!!

  • @shaharrefaelshoshany9442
    @shaharrefaelshoshany9442 2 ปีที่แล้ว

    this is the best explanation

  • @erickmedeirosbr
    @erickmedeirosbr 2 ปีที่แล้ว

    Very good

  • @ismail.bentour
    @ismail.bentour 2 ปีที่แล้ว

    very nice

  • @bensmyth450
    @bensmyth450 2 ปีที่แล้ว

    YOU ARE A SAVIOR

  • @joroh_
    @joroh_ 4 ปีที่แล้ว +1

    The best

  • @baatrmandzhiev3214
    @baatrmandzhiev3214 3 ปีที่แล้ว

    Great thanks

  • @oneloveafrica8860
    @oneloveafrica8860 4 วันที่ผ่านมา

    good to find u

  • @gokulkarthik9790
    @gokulkarthik9790 3 ปีที่แล้ว

    Really good..

  • @san-wh8dz
    @san-wh8dz 2 ปีที่แล้ว

    You sure hear this alot , but you are really so good ! thx you !

  • @taruntomar7795
    @taruntomar7795 2 ปีที่แล้ว +2

    That is such a nice explanation i love this guy

  • @user-gn7ho8yu1d
    @user-gn7ho8yu1d ปีที่แล้ว

    i love u

  • @georgegeorge581
    @georgegeorge581 3 ปีที่แล้ว

    Nice

  • @parisa9374
    @parisa9374 2 ปีที่แล้ว

    Thank you very much for your great explanation! Just curious what IDE are you using?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      Visual Studio Code. Here's a video on how you can also use it: code-vault.net/lesson/ublnbln8uf:1603733528013

  • @alexey104
    @alexey104 3 ปีที่แล้ว +1

    Great video, thanks!
    What if I want to redirect the standard output of a child process right into the parent process instead of redirecting it to a file? Can I use dup2() function with a pipe descriptor for that?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      Yeah, in basically the same way, you'll have to dup2 the standard input of the parent process to that pipe as well

  • @2644374
    @2644374 11 หลายเดือนก่อน

    It was really helpful to understand the basic. Could you please explain How to redirect pipe in and out to standard functions ?

    • @CodeVault
      @CodeVault  10 หลายเดือนก่อน

      The handles that you get for pipes should already work with any Linux function that uses this type of handle. As for the C standard functions, I will make a video on it

  • @lyricsmaker-poplyricsmaker1183
    @lyricsmaker-poplyricsmaker1183 4 ปีที่แล้ว +1

    I am more a c++ user but i really enjoy your videos and learn a lot from them! I have question: In c++ there is a function called freopen("filename", "w/r/a" stdin/stdout);
    Could that replace the whole dup2() thing in c;
    Is this a function works like dup2();

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +4

      This is interesting. Indeed that freopen function is equivalent to open + dup2.
      The interesting part is this can be found in stdio.h, so it's actually in the C standard not just the C++ standard.
      An important difference is that freopen works with streams (FILE* types) whereas dup2 works with file descriptors (int types) and is specific to UNIX.

    • @lyricsmaker-poplyricsmaker1183
      @lyricsmaker-poplyricsmaker1183 4 ปีที่แล้ว

      @@CodeVault ok thanaakss for answering my question!!!!

  • @stephenradmard9414
    @stephenradmard9414 2 ปีที่แล้ว

    Hey, thanks for your awesome videos!
    QUESTION: How do you go directly into a variable (e.g. char array) directly without opening a FILE?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      I don't understand the question. What does it mean to "go directly into a variable"?

    • @wilsonnuno
      @wilsonnuno 2 ปีที่แล้ว

      @@CodeVault I think he means saving the output of the ping command to a char array instead of creating a new file.
      If it is, I have the same doubt. For example I want to save the output of the command executed through execlp to a char array so I can then use the function strtok to get the different elements of the char array. Is it possible or do I have to save always to a file?

  • @igorthelight
    @igorthelight 2 ปีที่แล้ว

    For all Windows guys its:
    #include
    and
    _dup();
    or
    _dup2();

  • @cloudwalker2730
    @cloudwalker2730 ปีที่แล้ว

    In this case what we do in each process is then same, namely, increment the number by 1, so the order the process excecutes doesn't matter. But what if we do different things in process? For example, we want to multiply the number by 2, then add 2, then divide by 4, the order we do this definitely matters to the result, so how do we make sure the processes are excecuted in order? Thank in advance.

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      You can using pipes or using cross-process semaphores. This video:
      code-vault.net/lesson/sordz1xc2w:1603732432133
      and its continuation code-vault.net/lesson/v5cml4fg0w:1610639843712
      has a lot of the answers you are looking for

  • @ShivamSharma-nb6gh
    @ShivamSharma-nb6gh 3 ปีที่แล้ว

    I want to create a c/c++ program to run MULTIPLE (say 10) c/c++ programs please help 🙏

  • @d0rzA
    @d0rzA 5 หลายเดือนก่อน

    Will it work trying to use pipe with dup2(fd[1] , 1) and dup2(fd[0] , STDOUT) ?
    I want the pipe to write from stdin and read from stdout

    • @CodeVault
      @CodeVault  5 หลายเดือนก่อน +1

      Not really... You could write to a file the stdout of another and then read and redirect to a pipe

    • @d0rzA
      @d0rzA 5 หลายเดือนก่อน

      Thank you !
      @@CodeVault

  • @russlcorey
    @russlcorey 3 หลายเดือนก่อน

    Does closing stdout cause problems if the output of my program is being piped to a second program. Will it cause a broken pipe and can I avoid it by first duplicating stdout then restoring afterwards?

    • @CodeVault
      @CodeVault  3 หลายเดือนก่อน

      Yes, you could duplicate and restore the standard output

  • @user-si8bk3kz2z
    @user-si8bk3kz2z 2 ปีที่แล้ว

    how can i reproduce behaviour of < {file name} in c? which descriptors i should redirect?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      You could open the {file name} and just overwrite the stdin of the main process to be that file descriptor

  • @spyroschatziargyros3103
    @spyroschatziargyros3103 4 ปีที่แล้ว

    Helloo thanks for all of those video! But i whould like a course with more competitive programming with c and c++!!

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว

      Competitive programming? Can you give an examples of the types of videos you'd like to see?

  • @devanarayanan807
    @devanarayanan807 3 ปีที่แล้ว

    Nice work !! i love all the videos you posted
    I have one question from this video. If we overwriting STDOUT descriptor to custom file descriptor, i know some how it got reverted back (parent process printing in console). Could you explain why?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      What got printed to the console? Maybe it was from the STDERR

    • @devanarayanan807
      @devanarayanan807 3 ปีที่แล้ว

      @@CodeVault no like we tried printf in child process after dup2 performed then the printed data present in custom file, the after child returns parent process block executes printf that time data i s printed in STDOUT itself right ?

    • @devanarayanan807
      @devanarayanan807 3 ปีที่แล้ว

      I'm asking how the control for file descriptor 1(STDOUT->customfile) return back to STDOUT in parent process

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      Oh, wait. dup2 only changes the STDOUT file descriptor for the current process. If you've done that only for the child process, it won't affect the parent process at all.

    • @pauliuscernius5820
      @pauliuscernius5820 2 ปีที่แล้ว

      @@CodeVault But how do you revert it back if you ever need to do that?

  • @andelephantscanfly
    @andelephantscanfly 3 ปีที่แล้ว +1

  • @Victor_Marius
    @Victor_Marius 3 ปีที่แล้ว

    I think in windows you can redirect the output of a program in cmd to null to surpress it, but not sure if the same is true in c/c++

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      I'm not to keen on using the Win32 API as it's all over the place. But if you dig deep enough you should be able to find something like what is on Unix systems.

    • @Victor_Marius
      @Victor_Marius 3 ปีที่แล้ว

      @@CodeVault in windows there is a CreateProcess function which has ~10 arguments and the same is with other functions. That's why I learned python, it makes things easier

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      I'm not too familiar with python. How easy is it to create a process in Python?

    • @Victor_Marius
      @Victor_Marius 3 ปีที่แล้ว

      @@CodeVault in os module, you have os.system function which can execute a cmd command or any program. And also there is an subprocess module with "call", a very similar function to os.system. And I think there are more in the latter module I mentioned, but I know just what I used.
      os.system is ~like "execlp", if I said it's name right.

  • @efmflee
    @efmflee ปีที่แล้ว

    what if we dont close file after dup2(file, stdout_filenon); or it does not matter?

    • @CodeVault
      @CodeVault  ปีที่แล้ว +1

      The OS usually cleans up opened files descriptors after terminating the process but it's a good practice to close unused file descriptors (especially if it's a long-running process)

    • @efmflee
      @efmflee ปีที่แล้ว

      @@CodeVault thanks for reply :P

  • @k_ushagra
    @k_ushagra 3 ปีที่แล้ว

    now before the termination of child process, if I want to print something to stdout in child process only, how do we do that ?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      You need to use dup() in that case to store the old value. Here's a more in-depth answer: stackoverflow.com/questions/9084099/re-opening-stdout-and-stdin-file-descriptors-after-closing-them

    • @k_ushagra
      @k_ushagra 3 ปีที่แล้ว

      @@CodeVault its clear now, thanks for the help brother !

  • @steptoevs996
    @steptoevs996 3 ปีที่แล้ว

    So that means we can transfer date from an executable to another... now I can see more clear IPC.

  • @m.preacher2829
    @m.preacher2829 2 ปีที่แล้ว

    what if we first close the standard output and then create the file. would it also work?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      No, that wouldn't work

    • @m.preacher2829
      @m.preacher2829 2 ปีที่แล้ว

      @@CodeVault when the standard output is closed first, then the file descriptor after open( ) will be assigned to the smallest integer of file descriptor table( that would be 1). i have tried this by myself. the output of the command is written in the .txt file, but the output of printf( ) was not written in either the file or the console.

  • @sylvainducamp8447
    @sylvainducamp8447 2 ปีที่แล้ว

    I don't understand why you delete the "int file2 = " at the end, can you explain it more ? Great content btw !

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว +1

      Simply becuase the variable itself is not needed. We know file2 will always be STDOUT_FILENO in this situation so no need to store its value again

  • @MegaTheDarkdemon
    @MegaTheDarkdemon 3 ปีที่แล้ว

    How would you restore the fd of the stdout?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      Here's a good explanation on the topic: stackoverflow.com/questions/9084099/re-opening-stdout-and-stdin-file-descriptors-after-closing-them
      dup() is called to get a copy of the fd and then, they close the stdin/stdout. Right after that, by calling open(), they get the fd with id 0/1 since open uses the first available fd id

    • @MegaTheDarkdemon
      @MegaTheDarkdemon 3 ปีที่แล้ว

      @@CodeVault thank you

  • @darthvader4899
    @darthvader4899 2 ปีที่แล้ว

    How can i restore STDOUT to File Descriptior 1

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      Basically you have to use dup(1) to get the handler for the stdout before replacing it. Here's an explanation: stackoverflow.com/questions/11042218/c-restore-stdout-to-terminal

  • @naitside3410
    @naitside3410 2 ปีที่แล้ว

    это интересно. Единственное я не понимаю как работает файловый дескриптор. Я понимаю что два процесса НЕ могут иметь один и тот же файловый дескриптор с номером 3. Но как эта 3 хранит и передаёт открытый файл ? Это очень сложный вопрос, и если переводчик позволит тебе понять суть вопроса и ты ответишь, будет замечательно.

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว +1

      The file descriptor is just an identifier. Every function that uses that file descriptor does some translation to find where exactly is the file amongst other details. This file descriptor concept is basically there so you won't have to manually store everything the read/write functions need to function, just a simple integer.

  • @ashishsarkar5706
    @ashishsarkar5706 ปีที่แล้ว

    Great video . Where can i get the code ?

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      All the source codes for all the videos are available on the website: code-vault.net
      For this particular video, here is the link: code-vault.net/lesson/43zvcsz6o1:1603732432539

  • @beef2244
    @beef2244 3 ปีที่แล้ว

    Now your output appears in the VS Code terminal emulator. But at first it appeared in the actual Ubuntu command prompt. How did you make it appear in the Ubuntu command prompt?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      In the beginning of the course I was using a virtual machine with Ubuntu but I switched to code-server instead due to technical difficulties. There's a setting "externalConsole" in your launch.json that you have to set to true. But it has to be on a Ubuntu machine of course.

  • @thetruth65756
    @thetruth65756 2 หลายเดือนก่อน

    Who else is here from CS 341 at UIUC 😂

  • @davidzheng4683
    @davidzheng4683 3 ปีที่แล้ว

    why did he use 0777?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      It's just the permission code for having every type of user read/write/execute that file. (Albeit it's excessive in this case). Here's some documentation that should help out: www.linux.com/training-tutorials/understanding-linux-file-permissions/

  • @AlbertTulyakov
    @AlbertTulyakov 3 ปีที่แล้ว

    Create more video!!!

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      Very soon, my friend :D

  • @ericscharringa7201
    @ericscharringa7201 2 ปีที่แล้ว

    Very good video and exactly what I was looking for but still this C++ code doesn't work (date still shows up on screen instead of in file) :
    int shell(bool showPrompt) {
    pid_t child1 = fork();
    if (child1 == 0) {
    string naam = "eric.txt";
    const char *bestandnaam = naam.c_str();
    int bestand = open (bestandnaam, O_CREAT | O_WRONLY, 0777);
    if (bestand == -1) cout

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว +1

      The code looks fine... Can you send me the executeCommand function?

    • @ericscharringa7201
      @ericscharringa7201 2 ปีที่แล้ว

      @@CodeVault it just does a return execvp(date) and gives back an int as return code

    • @ericscharringa7201
      @ericscharringa7201 2 ปีที่แล้ว

      @@CodeVault at least thanks to your video I know I'm on the right track. Maybe something more to do with the virtual machine I am using for execution.