Using getline() with file input in C++

getline, as it name states, read a whole line, or at least till a delimiter that can be specified. So the answer is “no”, getlinedoes not match your need. But you can do something like:

Sleep for milliseconds

Note that there is no standard C API for milliseconds, so (on Unix) you will have to settle for usleep, which accepts microseconds:

How do I find the length of an array?

If you mean a C-style array, then you can do something like: This doesn’t work on pointers (i.e. it won’t work for either of the following): or: In C++, if you want this kind of behavior, then you should be using a container class; probably std::vector.

What is an undefined reference/unresolved external symbol error and how do I fix it?

Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference): The precedence among the syntax rules of translation is specified by the following phases [see footnote]. Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters for end-of-line indicators) … Read more

pinpointing “conditional jump or move depends on uninitialized value(s)” valgrind message

Use the valgrind option –track-origins=yes to have it track the origin of uninitialized values. This will make it slower and take more memory, but can be very helpful if you need to track down the origin of an uninitialized value. Update: Regarding the point at which the uninitialized value is reported, the valgrind manual states: It is important to … Read more

How to generate a random number in C++?

The most fundamental problem of your test application is that you call srand once and then call rand one time and exit. The whole point of srand function is to initialize the sequence of pseudo-random numbers with a random seed. It means that if you pass the same value to srand in two different applications (with the same srand/rand implementation) then you will get exactly the same sequence of rand() values read after … Read more

std::string to char*

It won’t automatically convert (thank god). You’ll have to use the method c_str() to get the C string version. Note that it returns a const char *; you aren’t allowed to change the C-style string returned by c_str(). If you want to process it you’ll have to copy it first: Or in modern C++:

How to throw a C++ exception

Simple: The Standard Library comes with a nice collection of built-in exception objects you can throw. Keep in mind that you should always throw by value and catch by reference: You can have multiple catch() statements after each try, so you can handle different exception types separately if you want. You can also re-throw exceptions: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)