96
u/MathSciElec Jul 01 '20
Terrible code indeed. They forgot the spaces between variables/constants and operators!
38
23
62
u/Noobnugget19 Jul 01 '20
I'm more upset about the bad spacing with = and super huge indents
6
9
u/BlazingThunder30 Jul 01 '20
What's wrong with tabspace = 8?
9
u/Mr2-1782Man Jul 01 '20
Some of use like to have two windows side by side.
-4
u/lestofante Jul 01 '20
Wich is exactly why 8 space tab is enforced in Linux, if you exceed the 80char limit like this, you probably affected by arrow code syndrome. If course general rule, not a universal law.
9
Jul 02 '20
Yep, Torvalds said once that if you need more than 3 indents, your code is probably flawed (ofc it's C, so no function definitions inside classes or namespaces). But they changed the 80 char hard limit to a soft limit last kernel update and the new hardlimit is now 100 chars per line.
3
8
4
3
3
Jul 02 '20
maybe use preprocessor #if and etc.? am i correct?
C++ noob here
2
u/TheSoundDude Jul 02 '20
For cross platform? Yeah, that would work. Using the proper filesystem library would be much better though, of course.
7
Jul 01 '20
This whole function is equivalent to
return filename.substr(0, filename.lastIndexOf('/'));
Edit: if youre writing in java don't know about cpp tho.
17
u/Breadfish64 Jul 01 '20
std::filesystem::path{filename}.parent_path();2
Jul 01 '20
What’s the implementation of lastIndexOf?
11
u/Breadfish64 Jul 02 '20
If you want the exact equivalent of the Java that would be
return filename.substr(0, filename.rfind('/'));1
u/eXl5eQ Jul 02 '20
This Java version throws OOB Exception if filename doesn't contain any '/'. (BTW in Java this method is called
substring, notsubstr). The C++ version returns the whole string1
Jul 02 '20
Shit your right. All you would have to add is a condition checking if substring returns -1.
1
1
1
u/Urtehnoes Jul 02 '20
God bless all languages that come with some form of Environment/OS/Application.PathSeparator
I ain't got time to figure out how these things are separated in a directory!!
1
0
95
u/mustaineTheDave Jul 01 '20 edited Jul 01 '20
Shouldn't i start at filename.size() - 1?
And is the reason it breaks on windows the fact that he accesses an index out of bounds? Why doesn't it break on other platforms too?
C/C++ noob here, genuinely want to know.
EDIT: I'm dumb, windows uses \