Redirecting standard output in C

Поделиться
HTML-код
  • Опубликовано: 16 май 2020
  • Check out our Discord server: / discord

Комментарии • 140

  • @XAyaDubX
    @XAyaDubX Год назад +55

    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

  • @sm8739
    @sm8739 2 года назад +4

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

  • @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.

  • @bama2619
    @bama2619 2 года назад +4

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

  • @lylesnodgrass1961
    @lylesnodgrass1961 Год назад +1

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

  • @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.

  • @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!!!

  • @LamNguyen-jp5vh
    @LamNguyen-jp5vh Год назад +1

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

  • @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..

  • @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.

  • @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)!!!

  • @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.

  • @t2a392
    @t2a392 Год назад

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

  • @hectordoyle4718
    @hectordoyle4718 4 года назад +2

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

  • @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

  • @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!!

  • @nivaspvs7220
    @nivaspvs7220 3 года назад

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

  • @oliviern.2095
    @oliviern.2095 2 года назад

    Great explanation, incredible. Thank you so much.

  • @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!!

  • @souhaylsoulami7591
    @souhaylsoulami7591 3 года назад +2

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

  • @noahrodriguez1895
    @noahrodriguez1895 3 года назад +1

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

  • @abhimanyumithun832
    @abhimanyumithun832 8 месяцев назад

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

  • @CodeWithEz
    @CodeWithEz 2 года назад

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

  • @sethsarchet1020
    @sethsarchet1020 2 года назад

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

  • @gracemcpadden3708
    @gracemcpadden3708 Месяц назад

    You are amazing! Thank you!

  • @dtantarjent144
    @dtantarjent144 Год назад

    Perfectly explained, great work!

  • @juggles5474
    @juggles5474 Год назад

    incredible video, thank you!!

  • @anstbt
    @anstbt Год назад

    life saver sir , thank you

  • @bahaeddineabdellawi1551
    @bahaeddineabdellawi1551 3 года назад

    thank you for this amazing video

  • @puissant218
    @puissant218 2 года назад

    Helped me a lot, thank you !

  • @multivalfran
    @multivalfran Год назад

    AMAZING. BEST CHANNEL EVER

  • @nom9d825
    @nom9d825 2 года назад +1

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

  • @santiagolaseul
    @santiagolaseul Год назад

    you are amazing.

  • @hylorial
    @hylorial Год назад

    YOU ARE THE MVP

  • @quocatle7140
    @quocatle7140 3 года назад

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

  • @rohailasad249
    @rohailasad249 3 года назад

    Thanks for the video. It helped a lot

  • @oussamazz09
    @oussamazz09 3 года назад

    thank you so much you for your efforts...

  • @theodeville8222
    @theodeville8222 2 года назад

    you are the big boss ty

  • @nofaith5410
    @nofaith5410 3 года назад +1

    I love you man you just saved me

  • @stormz4040
    @stormz4040 3 года назад

    Thank you for the video

  • @mahirkayadelen9202
    @mahirkayadelen9202 Год назад

    Good explanation thanks

  • @hamzabayar3547
    @hamzabayar3547 3 месяца назад

    it was helpful. Thanks a lot

  • @ahmetkemalbayraktar7017
    @ahmetkemalbayraktar7017 2 года назад

    thank you very much

  • @zinedineaitaider5229
    @zinedineaitaider5229 3 года назад

    very good explanation thank you

  • @stevendam8031
    @stevendam8031 3 года назад

    thank you mate

  • @phyroxdimebag3862
    @phyroxdimebag3862 2 года назад

    awesome video!

  • @shaharrefaelshoshany9442
    @shaharrefaelshoshany9442 2 года назад

    this is the best explanation

  • @wenlix8690
    @wenlix8690 3 года назад

    ty very much you're the best (again)

  • @2644374
    @2644374 8 месяцев назад

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

    • @CodeVault
      @CodeVault  8 месяцев назад

      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

  • @bensmyth450
    @bensmyth450 2 года назад

    YOU ARE A SAVIOR

  • @lionbursh6342
    @lionbursh6342 3 года назад

    Thank you!!!

  • @isikozsoy
    @isikozsoy 3 года назад

    Thanks a lot!

  • @joroh_
    @joroh_ 4 года назад +1

    The best

  • @kar3lio
    @kar3lio 2 года назад

    Muy buenos videos, me han ayudado mucho 👍

  • @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!!!!

  • @erickmedeirosbr
    @erickmedeirosbr 2 года назад

    Very good

  • @igorthelight
    @igorthelight 2 года назад

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

  • @baatrmandzhiev3214
    @baatrmandzhiev3214 2 года назад

    Great thanks

  • @ismail.bentour
    @ismail.bentour 2 года назад

    very nice

  • @kritonios
    @kritonios 6 месяцев назад

    Very nice video

  • @techgnosis3693
    @techgnosis3693 3 года назад

    Good stuff broo...

  • @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

  • @taruntomar7795
    @taruntomar7795 2 года назад +2

    That is such a nice explanation i love this guy

  • @gokulkarthik9790
    @gokulkarthik9790 3 года назад

    Really good..

  • @san-wh8dz
    @san-wh8dz 2 года назад

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

  • @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?

  • @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  2 года назад +1

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

  • @ShivamSharma-nb6gh
    @ShivamSharma-nb6gh 2 года назад

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

  • @georgegeorge581
    @georgegeorge581 3 года назад

    Nice

  • @devanarayanan807
    @devanarayanan807 2 года назад

    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  2 года назад

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

    • @devanarayanan807
      @devanarayanan807 2 года назад

      @@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 2 года назад

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

    • @CodeVault
      @CodeVault  2 года назад

      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 Год назад

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

  • @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

  • @user-gn7ho8yu1d
    @user-gn7ho8yu1d Год назад

    i love u

  • @d0rzA
    @d0rzA 3 месяца назад

    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  3 месяца назад +1

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

    • @d0rzA
      @d0rzA 3 месяца назад

      Thank you !
      @@CodeVault

  • @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

  • @steptoevs996
    @steptoevs996 3 года назад

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

  • @russlcorey
    @russlcorey Месяц назад

    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  26 дней назад

      Yes, you could duplicate and restore the standard output

  • @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?

  • @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.

  • @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 !

  • @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

  • @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

  • @andelephantscanfly
    @andelephantscanfly 2 года назад +1

  • @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.

  • @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.

  • @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

  • @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

  • @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

  • @thetruth65756
    @thetruth65756 14 дней назад

    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/

  • @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.

  • @AlbertTulyakov
    @AlbertTulyakov 3 года назад

    Create more video!!!

    • @CodeVault
      @CodeVault  3 года назад

      Very soon, my friend :D