I'm more of a C guy. What's the idiomatic C++11 or 14 way of doing this?

I'm more of a C guy. What's the idiomatic C++11 or 14 way of doing this?
Usually in this situation I'd use a FILE* initialized to stdin and then if there was an argument in argv I'd fopen it, check the return and go on with my day.
I'm trying to use the stack to free things for me; RAII or whatever it's called. I don't really need real_fin hanging around if i'm operating on stdin obviously.
sizeof(real_fin) gives me 520
#include #include using namespace std;intmain(int argc, char **argv){ istream *fin = &cin; ifstream real_fin; if (argc == 2) { real_fin.open(argv[1]); if (real_fin.bad()) return 1; fin = &real_fin; } cout

Other urls found in this thread:


Isn't RAII just plain stack scope shit but with extra features? Like real_fin doesn't go out of scope so I don't know what you are expecting.

What you've written is a mess. How about first stating what you're actually trying to do?

What's so hard about reading that? It's just a few lines of a simple test of copying an input stream to stdout.
Basically I want to do this.
intmain(int argc, char **argv){ FILE *fin = stdin; if (argc == 2) { fin = fopen(argv[1], "r"); if (!fin) return 1; } puts("no stupid std::ifstream class" " hanging around on the stack" " if I'm only using stdin"); func_that_doesnt_return_for_a_few_weeks(); fclose(fin); return 0;}
And I don't want to explicitly free anything.


Factoring the operation into a function that takes a reference to the base class type is probably the easiest way to do it.

#include #include voiddump(std::istream& in){ std::cout

Use Rust. I would post the Rust version but I can't understand this braindamage that you posted.
C/C++ are such ugly languages.

The truth behind Rustfags revealed

We will soon live in a world without cnility. You are deprecated and considered harmful.

You can get ahead of the curve by uninstalling your operating system.

This but unironically, everything should run ontop of UEFI

We are currently incrementally rewriting GNU/Linux.


allow me to help. The OP's intent is to write the C++ version of this code::- module lol.:- interface.:- import_module io.:- pred main(io::di, io::uo) is det.:- implementation.:- import_module list, exception.:- pred dump(input_stream::in, io::di, io::uo) is det.main(!IO) :- io.command_line_arguments(Args, !IO), ( Args = [File] -> io.open_input(File, Res, !IO), ( Res = ok(Stream) ; Res = error(E), throw(E) ) ; Stream = io.stdin_stream ), dump(Stream, !IO).dump(S, !IO) :- io.read_line_as_string(S, Res, !IO), ( Res = ok(Line), io.write_string(Line, !IO), dump(S, !IO) ; Res = eof ; Res = error(Err), throw(Err) ).

sudo apt remove rust* libstd-rust* cargo*

sudo apt remove snapd* libsnapd*

sudo apt remove firefox thunderbird


If you really care about the stack size, you could also do this.
#include #include #include voiddump(std::istream& in){ std::cout

Can you not just move real_fin into the if statement?


Don't forget bzip2
Anti Rust shills on suicide watch



Attached: rsvg-bench-timings.png (605x340, 5.53K)

This looks like the most correct answer. Thanks.

So basically all that refactoring and work produced no discernible advantage. Nice to know.

You're welcome. Keep in mind, while the std::ifstream won't be initialized unless needed, space for the object will still be reserved on the stack in main(). If you really must avoid stack allocation altogether, use this one: . (but remember to check for allocation failure before dereferenceing, unlike the example).
if (!fin || !*fin) return 1;.

the general idea is that the software is safer and easier to work with, while losing no speed. So you can apply new optimizations in the future, or add on new features, that you would have had to avoid if you'd stuck with C.
The problem with this idea is that Rust is harder to work with than C.

What are you trying to do OP, simply dump the contents of a textfile?#include #include #include int main() { std::string filename{"FooBar.txt"}, line{}; std::ifstream ifs{filename}; while (std::getline(ifs, line)) std::cout