Found 7346 Articles for C++

What is the best way to read an entire file into a std::string in C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

2K+ Views

This is an simple way to read an entire file into std::string in C++AlgorithmBegin    Take the filename as inputstream.    Declare a string variable str.    Read the file till the end by using rdbuf().    Put the data into st.    Print the data. End.Example Code#include #include #include #include using namespace std; int main() {    ifstream f("a.txt");    string str;    if(f) {       ostringstream ss;       ss

Reading and writing binary file in C/C++

Samual Sam
Updated on 26-Jun-2024 23:28:09

60K+ Views

Writing To write a binary file in C++ use write() method. It is used to write a given number of bytes on the given stream, starting at the position of the "put" pointer. The file is extended if the put pointer is currently at the end of the file. If this pointer points into the middle of the file, characters in the file are overwritten with the new data. If any error has occurred during writing in the file, the stream is placed in an error state. Syntax of write() method ostream& write(const char*, int); Reading To read a binary ... Read More

How to listdown all the symbols in a .so file in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

651 Views

To read a .so file in elf format, use readelfreadelf -Ws libName.soIt helps to extract symbol from binary.The standard tool used for listing all symbol is, nmnm -g libName.so

How can I get the list of files in a directory using C or C++?

karthikeya Boyini
Updated on 30-Jun-2020 10:47:33

12K+ Views

Let us consider the following C++ sample code to get the list of files in a directory.AlgorithmBegin    Declare a poniter dr to the DIR type.    Declare another pointer en of the dirent structure.    Call opendir() function to open all file in present directory.    Initialize dr pointer as dr = opendir(".").    If(dr)       while ((en = readdir(dr)) != NULL)          print all the file name using en->d_name.       call closedir() function to close the directory. End.Example#include #include #include using namespace std; int main(void) {    DIR ... Read More

Why is address zero used for the null pointer in C/C++?

Samual Sam
Updated on 30-Jul-2019 22:30:25

497 Views

Null pointer is a pointer which points nothing.Some uses of null pointer are:b) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.b) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.c) To check for null pointer before accessing any pointer variable. So that, we can perform error handling in pointer related code e.g. dereference pointer variable only if it’s not NULL.In C++ if we assign 0 in any pointer that means the pointer pointing to the NULL.SyntaxFloat *p = 0 //initializing the pointer ... Read More

C++ Program for Inorder Tree Traversal without Recursion

Anvi Jain
Updated on 30-Jul-2019 22:30:25

518 Views

If a binary tree is traversed in-order, the left subtree is visited first, then the root and later the right sub-tree. The output the key in ascending order in in_order traversal. This is a C++ Program for Inorder Tree Traversal without Recursion.AlgorithmBegin      Function inOrder():       Declare a stack s.       Declare the current node as root.       While current node is not null and stack is not empty do          While current node is not null do             Push the current node on the ... Read More

C++ Program to Implement Array in STL

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

415 Views

Different operations on array and pseudocodes:Begin In main(),    While TRUE do       Prints some choices.       Take input of choice.       Start the switch case          When case is 1             Print the size of the array.             Break          When case is 2             Insert the values in array.             Break          When case is 3             Print ... Read More

vector::begin() and vector::end() in C++ STL

Anvi Jain
Updated on 30-Jul-2019 22:30:25

4K+ Views

vector::begin() function is a bidirectional iterator used to return an iterator pointing to the first element of the container.vector::end() function is a bidirectional iterator used to return an iterator pointing to the last element of the container.AlgorithmBegin    Initialize the vector v.    Declare the vector v1 and iterator it to the vector.    Insert the elements of the vector.    Print the elements. End.Example Code Live Demo#include #include using namespace std; int main() {    vector v = { 50, 60, 70, 80, 90}, v1; //declaring v(with values), v1 as vector.    vector::iterator it;    //declaring an ierator ... Read More

vector insert() function in C++ STL

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

662 Views

vector insert() function in C++ STL helps to increase the size of a container by inserting the new elements before the elements at the specified position.It is a predefined function in C++ STL.We can insert values with three types of syntaxes1. Insert values by mentioning only the position and value:vector_name.insert(pos, value);2. Insert values by mentioning the position, value and the size:vector_name.insert(pos, size, value);3. Insert values in another empty vector form a filled vector by mentioning the position, where the values are to be inserted and iterators of filled vector:empty_eector_name.insert(pos, iterator1, iterator2);AlgorithmBegin    Declare a vector v with values.    Declare ... Read More

unordered_multimap swap() function in C++ STL

Anvi Jain
Updated on 30-Jul-2019 22:30:25

99 Views

unordered_multimap swap() function in C++ STL is used to swap the elements of one multimap to another of same size and type.AlgorithmBegin    Declaring two empty map container m, m1.    Insert some values in both m, m1 map containers.    Perform swap() function to swap the values of m, m1 map containers.    Printing the swapped values of m map container.    Printing the swapped values of m1 map container. End.Example Code Live Demo#include #include using namespace std; int main() {    unordered_map m, m1; // declaring m, m1 as empty map container    m.insert (pair('b', 10)); ... Read More

Advertisements