[ / / / / / / / / / / / / / ] [ dir / ausneets / cow / cyber / india / loomis / mu / sl / wis ][Options][ watchlist ]

/tech/ - Technology

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Email
Comment *
File
Select/drop/paste files here
Password (Randomized for file and post deletion; you may also set your own.)
Archive
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

[–]

 No.1070817>>1070823 >>1070827 [Watch Thread][Show All Posts]

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

____________________________
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070823

>>1070817 (OP)

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.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070827>>1070829

>>1070817 (OP)

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070829>>1070830 >>1070832 >>1070892

>>1070827

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070830

>>1070829

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070832>>1071061

>>1070829

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070859>>1070862 >>1070887

Use Rust. I would post the Rust version but I can't understand this braindamage that you posted.

C/C++ are such ugly languages.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070862>>1070864 >>1070885

>>1070859

>too stupid to understand C++

The truth behind Rustfags revealed

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070864>>1070867

>>1070862

>not having learned a language makes you stupid

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070867>>1070872 >>1070874

>>1070864

>We will soon live in a world without c

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070872>>1070874

>>1070867

This but unironically, everything should run ontop of UEFI

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070874

>>1070867

We are currently incrementally rewriting GNU/Linux.

>>1070872

>(((UEFI)))

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070885

>>1070862

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070887>>1070890 >>1070907

>>1070859

sudo apt remove rust* libstd-rust* cargo*

sudo apt remove snapd* libsnapd*

sudo apt remove firefox thunderbird

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070890>>1070901

>>1070887

>apt

>reddit spacing

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070892>>1071064

>>1070829

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070900

Can you not just move real_fin into the if statement?

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070901

>>1070890

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1070907>>1071016

>>1070887

Don't forget bzip2

https://people.gnome.org/~federico/blog/maintaining-bzip2.html

Anti Rust shills on suicide watch

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1071016>>1071020

>>1070907

>rustification

>making software slower on purpose

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1071020>>1071062

File (hide): 74bbc6d89180423⋯.png (5.53 KB, 605x340, 121:68, rsvg-bench-timings.png) (h) (u)

>>1071016

https://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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1071061>>1071064

>>1070832

This looks like the most correct answer. Thanks.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1071062>>1071073

>>1071020

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1071064

>>1071061

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: >>1070892. (but remember to check for allocation failure before dereferenceing, unlike the example).

if (!fin || !*fin) return 1;
.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1071073

>>1071062

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.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.1071427

>24 responses, no answer

>the absolute state of /tech/

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

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.



[Return][Go to top][Catalog][Screencap][Nerve Center][Update] ( Scroll to new posts) ( Auto) 5
25 replies | 1 images | Page ???
[Post a Reply]
[ / / / / / / / / / / / / / ] [ dir / ausneets / cow / cyber / india / loomis / mu / sl / wis ][ watchlist ]