Linux programming in C++ [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've just finished reading Accelerated C++ so I'm still new to C++, but I want to learn Unix/Linux programming (as in command line applications and daemons). I've seen everyone recommending Advanced Programming in Unix Environment but it seems it's C-oriented. I know C++ is drawn from C so I can use every function in book in my C++ project, but for example - when it comes to opening a file: should I use open() from < fcntl.h> (as stated in book), or from < fstream>? Same goes with other functions in book. So, question is whether and when to use POSIX C functions when there are also "corresponding/matching" C++ functions.

The Unix API is C (as is the Windows API); any calls into Unix will look
like C. That doesn't stop you from using C++, however: if the function
requires a char const*, for example, you can call
std::string::c_str() on an std::string.
Whether you want the Unix function (e.g read()) or the C++ (>> on a
stream) depends on what you need. The C++ functionality is generally at
a higher level; read() will only read an array of bytes, for example,
and will not parse integers in text format. On the other hand, you
have a lot more low level control with read() and its assorted
functions; if you need transactional itegrity, for example, you'll need
to use open(), passing it the approriate flags, which aren't available
in std::fstream::open(). More generally, the C++ functions that
involve interaction with the exterior (or with other threads) are built
on the underlying Unix system calls. They provide a higher level of
abstraction, and will generally be simpler to use, but in specific
cases, they may not offer all of the functionality available at the lower levels.

Related

C++ Iterating through files in a directory: Is it bad practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
The question has been asked many times here: "How do I iterate through files in a directory in c++?"
I've seen the responses, typically suggesting to use an external library (usually boost filesystem) to handle this and the OS differences in implementation.
My question is: why? Why is this so difficult?
I'm new to the language and can't help but feel that I'm trying to overstep the bounds of idiomatic c++.
Is it more appropriate to implement a single file solution and use another language to implement the file iteration?
Relative to other languages, C++ has a tiny standard library. This has its advantages (porting C++ to a new platform is much easier), but it also means that, to get a lot of things done, you're going to rely on external libraries.
Filesystem work has not, until C++17, been something that has been a part of the C++ standard. And even now, some people are resistant to the C++17 filesystem library because it doesn't work quite so well with certain types of underlying filesystems.
Note that many languages that have standard filesystem support don't support these platforms at all.
I'm new to the language and can't help but feel that I'm trying to overstep the bounds of idiomatic c++.
If you're going to use C++, then you need to accept that you're going to have to go out and use other libraries for a lot of the stuff that many other languages give you for free. Using a library is how you get things done in C++; it's not "overstepping the bounds" of anything.
You can't iterate the files in a directory in C++ without using platform-specific APIs. The core C++ language and the standard library do not provide any mechanism to obtain a directory listing and iterate through it. So in order to do this, you must use an external library or platform specific API.

c++ cout vs printf() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
After learning about c++ through a couple different sources, I found conflicting advice concerning the use of cout/printf(). One source said that printf(), and I quote:
... does not provide type safety, so it is easy to inadvertently tell it to display an integer as if it were a character and vice versa. printf() also does not support classes, and so it is not possible to teach it how to print your class data; you must feed each class member to printf() one by one.
So, the more important thing to me would be the readability factor of using printf(). On the other hand, another source mentioned that cout, with its use of the overloaded operator <<, uses more instructions to execute and can therefore be more expensive in terms of memory over a large program. Although, the person who said this was a systems programmer, in which every bit of performance is vital. But say I wanted to go into game or application development.
Would the performance differences between printf() and cout matter all that much?
In general, does it really matter what I choose to use in an application program?
Thank you for any input.
You would measure the differences on your particular implementation for your specific use case and determine that for yourself.
I would say both lines of reasoning in the question have merit, but you can't generalise about performance.
If you want to go into game or application programming, printf/cout won't be needed too much. The only use in these cases is for debugging.
If you really need to use printf/cout a lot, the difference will be when writing a huge amount of data, otherwise you don't need to bother.

should I mix C & C++ style I/O in my C++ program? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
This question confuses me lot. Because C++ is a superset of C programmer is free to use C's library functions like printf(), scanf() & many others etc. But I usually like C++ 's Object oriented I/O system & I mostly like to use cout & cin. Because iostream is more type safe, less error prone, extensible, flexible & also inheritable. Should I stop using traditional C's I/O functions because of iostream 's advantages or should I modify my program to use ? Which approach is better? Where should I take care when mixing C & C++ I/O. I know that backward compatibility with legacy C programs is necessary, but what should I really do?
C++ and C streams are synchronized by default, so you can mix them safely. This behavior is controlled by std::ios_base::sync_with_stdio.
As to whether you should do it? Doesn't matter. C++ does not have a universal style guide. Some programmers prefer the C++ iostreams interface, some prefer C's methods, some mix them.
Here are some links that discuss the problem better than I can.
Should I switch to C++ I/O streams?
The Duct Tape Programmer - Joel Spolsky

Implementation of C string functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to know how the string manipulation functions are implemented internally so I can figure out their performance. Is there a way to find this out?
I'm talking about null-terminated c strings (arrays of chars) and the related functions (strcat and such) in C++, if that has anything to do with it.
Is there a way to find this out?
Use the source, Luke
The sources for GNU libc string functions are easily viewable in the Git repository
You can also look at other free software or open source C libraries, such as newlib, FreeBSD, NetBSD, OpenBSD, OpenSolaris etc.
if your whole purpose is to figure out the performance I don't think you need to know how it is implemented. You can feed in different data and come up with a graph and compare how the functions performed.
But if you need to study how those functions are implemented, then there is always the source code, which you can get from the internet for different C++ compilers( Not all compilers though).

Polymorphic engines in C or C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I stumbled across polymorphic engines and I don't know anything about them. However, I am curious about how they are written. Every example that I've looked up writes them in assembly, my assembly is not good at all; I know just a few instructions here and there but not that well. On the other hand, I am good in C and C++.
I am familiar with the concept of polymorphism in C++ but after reading about polymorphic engines, I am assuming that they are different from the polymorphism in C++.
How can techniques such as using virtual keyword in C++ be used to obfuscate or encrypt the code in an application?
If a program has to be modified you can go either modifying the source code or modifying the compiled executable.
The first approach is awful (in my opinion) because:
A source file is subject to a lot of optimizations in the compilation processes. So two source files slightly different from each other could produce the same object code.
If you need your program to be self modifying you will have to carry with all the tools needed to build it. (Something like carrying a candy factory with you just for the case you want a candy of a different flavor in your trip)
...
Notice that I'm talking here about compiled languages as the use of C or C++ in your question suggests. For interpreted languages the first approach is the obvious one.
In your case, the second makes more sense but it is strictly related to the machine code of the target machine.
So my point is: if you want to implement a program or routine that is able to produce a modified version of other program or a modified version of itself you can implement it in Assembly, C, C++ or any other language but in all cases you have to be proficient in your target machine's assembly language and machine code.
I recommend you to research more. This topic is broad. In the case you decide to go on, I can say that Assembly won't be the biggest dragon to beat.