Spyke

When using command line arguments, is better to use them as c strings and interact with them using the cstring header functions?, or is better to convert them to library strings for using them?

im new in c++ and i am creating a version of cat for practicing what i have learned so far. What im doing for managing the command line arguments is converting them to library strings and then using them, but now i have the doubt if it is the correct / most optimal way to do it

View original on lemmy.world

You can quickly get the args into a vector like this:

auto args = std::vector<std::string_view>(argv, argv + argc);

Checking equality etc directly instead of using strcmp stuff is better. There are libraries available for handling command line args too.

4
lemmy.world

Use std::string_view to sort of get the safety of std::string without copying the contents (just in general make sure the original c string won't get freed or overwritten, which won't happen to argv in your case).

Or just std::string and yolo, the overhead of copying the contents is negligible in your use case.

4
SuperFolareply
programming.dev

If they are just practicing it isn't a problem, C++17 is already 6 years old and the open source community should get onboard imo

1

I'm not talking about professional projects. OP asked about making a pet project in C++.

Also, a lot of opensource software could jump forward in term of standard required ; I'm not saying that no professional project uses opensource library, just that we should start moving forward if we don't want to get stuck with old versions

2

You reached the end

When using command line arguments, is better to use them as c strings and interact with them using the cstring header functions?, or is better to convert them to library strings for using them? | Spyke