[C(++)] strings in file to string in memory

Started by
10 comments, last by ToohrVyk 16 years, 1 month ago
The +96 is not an encryption at all, since its completely irrelevant for the problem I didn't explain why I use it.

Why would I write it in C++ when I don't use all those extra functions they provide? What is wrong with C? It wasn't very difficult after all, was similair difficult in C++, why the hate towards plain C if C++ doesn't add a significant ammount of usefullness?

And why would I rewrite my entire function as a WORKAROUND, since I couldn't get it to work in C? That would be a waste of time and effort.

Tell me, why would I use a canon to fire a mosquito while I could aswell hit it with my hands?

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
Quote:Original post by Decrius
Why would I write it in C++ when I don't use all those extra functions they provide? What is wrong with C? It wasn't very difficult after all, was similair difficult in C++, why the hate towards plain C if C++ doesn't add a significant ammount of usefullness?


You're wrong. Writing the C code was an order of magnitude harder than C++: I'm a fluent programmer in both, yet I still had to look up the reference for strncpy and strncat to determine what their last argument represented, in addition to the effort I had to spend to make the code exception-proof. Even then, the code remains quite long, especially when the corresponding C++ code is:

const std::string path = "data/", ext = ".png";std::string input;std::vector<std::string> images;while (std::getline(file,input,0) && !input.empty())  images.push_back(path + input + ext);load_textures(images);


There are exactly zero potential overflow or memory leak issues in this code, and I made absolutely no effort to make sure of it. By comparison, the C code contained one potential memory leak (new []), four potential string overflows (strncpy, strncat, plus the size of buffer), one of which was actually present in the code before I checked the manual for strncpy again and added the assert, and one potential array overflow (since you have to pass the size alongside the array).

Quote:And why would I rewrite my entire function as a WORKAROUND, since I couldn't get it to work in C? That would be a waste of time and effort.


Code quality improvement is not a workaround. Besides, since the C++ code is shorter and faster to write and debug, you certainly didn't waste any time or effort.

Quote:Tell me, why would I use a canon to fire a mosquito while I could aswell hit it with my hands?


Incorrect analogy. Using C string manipulation functions in C++ at your level of skill (after all, you couldn't solve this problem on your own) is akin to hitting a mosquito with a Ming vase: you'll have trouble hitting your target, and there'll be a lot of pain if you miss. So, use your hands instead.

This topic is closed to new replies.

Advertisement