I'm more of a C guy

Benjamin Fisher
Benjamin Fisher

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 <iostream>
#include <fstream>

using namespace std;

int
main(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 << "dumping" << endl;
cout << fin->rdbuf();
cout << endl << "done" << endl;
return 0;
}

Other urls found in this thread:

people.gnome.org/~federico/blog/maintaining-bzip2.html
people.gnome.org/~federico/blog/rsvg-bench.html

Liam Walker
Liam Walker

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.

Logan King
Logan King

What's the idiomatic C++11 or 14 way of doing this?
What you've written is a mess. How about first stating what you're actually trying to do?

Tyler Anderson
Tyler Anderson

What you've written is a mess
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.

int
main(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.

Jason Mitchell
Jason Mitchell

s/func_that_doesnt_return_for_a_few_weeks();/func_that_doesnt_return_for_a_few_weeks(fin);/

Jordan Wilson
Jordan Wilson

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

#include <fstream>
#include <iostream>

void
dump(std::istream& in)
{
std::cout << "dumping" << std::endl;
std::cout << in.rdbuf();
std::cout << std::endl << "done" << std::endl;
}

int
main(int argc, char* argv[])
{
if (argc == 2) {
std::ifstream ifs(argv[1]);
if (!ifs) return 1;
dump(ifs);
}
else {
dump(std::cin);
}
return 0;
}

Ian Parker
Ian Parker

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

Isaiah Clark
Isaiah Clark

too stupid to understand C++
The truth behind Rustfags revealed

Mason King
Mason King

not having learned a language makes you stupid
We will soon live in a world without cnility. You are deprecated and considered harmful.

Dominic Harris
Dominic Harris

We will soon live in a world without c
You can get ahead of the curve by uninstalling your operating system.

Grayson Kelly
Grayson Kelly

This but unironically, everything should run ontop of UEFI

Luke Hill
Luke Hill

We are currently incrementally rewriting GNU/Linux.

(((UEFI)))

Isaac Johnson
Isaac Johnson

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)
).

Adrian Long
Adrian Long

sudo apt remove rust* libstd-rust* cargo*

sudo apt remove snapd* libsnapd*

sudo apt remove firefox thunderbird

Dylan Martin
Dylan Martin

apt
reddit spacing

Ryan Gomez
Ryan Gomez

If you really care about the stack size, you could also do this.
#include <fstream>
#include <iostream>
#include <memory>

void
dump(std::istream& in)
{
std::cout << "dumping" << std::endl;
std::cout << in.rdbuf();
std::cout << std::endl << "done" << std::endl;
}

int
main(int argc, char* argv[])
{
if (argc == 2) {
std::unique_ptr<std::ifstream> fin{new std::ifstream(argv[1])};
if (!*fin) return 1;
dump(*fin);
}
else {
dump(std::cin);
}
return 0;
}

Carson Robinson
Carson Robinson

Can you not just move real_fin into the if statement?

Dylan Campbell
Dylan Campbell

a year on and this retarded nigger still thinks the compiler is a dependency for compiled rust code

Thomas Price
Thomas Price

Don't forget bzip2
people.gnome.org/~federico/blog/maintaining-bzip2.html
Anti Rust shills on suicide watch

Jaxson Davis
Jaxson Davis

rustification
making software slower on purpose

Brandon Jackson
Brandon Jackson

people.gnome.org/~federico/blog/rsvg-bench.html
2.40.20 - the old C-only version
2.42.0 - C + Rust, with a lalrpop parser for the transform attribute
2.42.1 - Servo's cssparser for the transform attribute
2.42.2 - removed most C-to-Rust string copies during parsing

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

Asher James
Asher James

This looks like the most correct answer. Thanks.

Lincoln Brown
Lincoln Brown

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

Hunter Kelly
Hunter Kelly

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;.

Liam Wilson
Liam Wilson

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.

Christopher Hernandez
Christopher Hernandez

24 responses, no answer
the absolute state of Zig Forums
What are you trying to do OP, simply dump the contents of a textfile?#include <fstream>
#include <iostream>
#include <string>

int main() {
std::string filename{"FooBar.txt"}, line{};
std::ifstream ifs{filename};
while (std::getline(ifs, line))
std::cout << line << '\n';
}
is the idiomatic way of doing this in C++. You have deterministic destruction of all containers (such as filestreams & strings) at the function's closing }.