• Oinks@lemmy.blahaj.zone
    link
    fedilink
    English
    arrow-up
    20
    ·
    edit-2
    1 day ago

    I am very sorry to remind everyone about the existence of Visual Basic, but it has:

    • VbCrLf
    • VbNewLine
    • ControlChars.CrLf
    • ControlChars.NewLine
    • Environment.NewLine
    • Chr(13) & Chr(10)

    And I know what you’re asking: Yes, of course all of them have subtly different behavior, and some of them only work in VB.NET and not in classic VB or VBA.

    The only thing you can rely on is that “\r\n” doesn’t work.

  • ulterno@programming.dev
    link
    fedilink
    English
    arrow-up
    18
    ·
    edit-2
    20 hours ago

    Simple. \n when you just want a newline.
    endl when you need to flush at the moment.

    Useful in case you are printing a debug output right before some function that might do bed stuff to buffers.


    Edit: I wrote println instead of endl somehow. Guess I need more downtime

      • pelya@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        19 hours ago

        It depends on whether you are printing to a terminal or to a file (and yes the terminal is also a file), and even then you can control the flushing behaviour using something like unbuffer

    • Rednax@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      8 hours ago

      Considering std::cout should only directly be used when you are too lazy to place breakpoints, I totally get the decision to auto-flush.

    • mmddmm@lemm.ee
      link
      fedilink
      arrow-up
      34
      ·
      2 days ago

      It’s a very C++ thing that the language developers saw the clusterfuck that is stream flushing on the kernel and decided that the right course of action was to create another fucking layer of hidden inconsistent flushing.

      • jdeath@lemm.ee
        link
        fedilink
        arrow-up
        2
        ·
        11 hours ago

        programmers manage to do stupid shit in every language. i was wondering if there was a way to stop them, and golang comes close but maybe proves it can’t be done. idk!

    • unalivejoy@lemm.ee
      link
      fedilink
      English
      arrow-up
      20
      ·
      2 days ago

      Just because the box says something is flushable doesn’t mean you should flush it.

  • pelya@lemmy.world
    link
    fedilink
    arrow-up
    29
    ·
    edit-2
    2 days ago

    printf is superior and more concise, and snprintf is practically the only C string manipulation function that is not painful to use.

    Try to print a 32-bit unsigned int as hexadecimal number of exactly 8 digits, using cout. You can do std::hex and std::setw(8) and std::setfill('0') and don’t forget to use std::dec afterwards, or you can just, you know, printf("%08x") like a sane person.

    Just don’t forget to use -Werror=format but that is the default option on many compilers today.

    C++23 now includes std::print which is exactly like printf but better, so the whole argument is over.

    • SqueakyBeaver@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      3
      ·
      19 hours ago

      I went digging in cppref at the format library bc I thought c++20 or c++23 added something cool.

      Found std::print and was about to reply to this comment to share it bc I thought it was interesting. Then I read the last sentence.

      Darn you and your predicting my every move /j

    • Fonzie!@ttrpg.network
      link
      fedilink
      arrow-up
      1
      ·
      1 day ago

      For me the answer is “Building backend applications with it instead of CLI applications, like Lerdorf intended.”

      But also "\n" because it’s easier and PHP_EOL is just an alias for "\n"; it’s not even platform-dependent.

      • Rikudou_Sage@lemmings.world
        link
        fedilink
        arrow-up
        2
        ·
        1 day ago

        PHP_EOL depends on your host system, it’s \r\n on Windows.

        I don’t really want to use what Lerdorf intended, PHP <= 4 was horrible, 5.x was mainly getting slowly rid of nonsense and with 7.x PHP started its slow path of redemption and entered its modern era.

        While Lerdorf’s vision was great at that time for its intended use case, I wouldn’t want to build anything serious in it.

        • Fonzie!@ttrpg.network
          link
          fedilink
          arrow-up
          1
          ·
          1 day ago

          It actually outputs "\n" on a Windows system, but modern Windows to recognise that as enough of a newline, nowadays.

          I don’t really want to use what Lerdorf intended, PHP <= 4 was horrible

          Actually a great point!

  • r00ty@kbin.life
    link
    fedilink
    arrow-up
    3
    ·
    2 days ago

    Maybe c# has similar. There’s \r\n or \n like c++ and Environment.NewLine.

    Probably it’s similar in that Environment.NewLine takes into account the operating system in use and I wonder if endl in c++ does the same thing?

    • vithigar@lemmy.ca
      link
      fedilink
      arrow-up
      4
      ·
      1 day ago

      C# also has verbatim strings, in which you can just put a literal newline.

      string foo = @"This string 
      has a line break!";
      
  • Sibbo@sopuli.xyz
    link
    fedilink
    arrow-up
    3
    ·
    2 days ago

    Wasn’t this {fmt} library merged into STL now? Does this solve this issue?

    Anyways, there was also a constant that is the OS line ending without a flush, right?