Found 7347 Articles for C++

Read integers from a text file with C++ ifstream

Smita Kapse
Updated on 30-Jul-2019 22:30:26

6K+ Views

Here is an example of Read integers from a text file with C++ ifstream.Example#include #include using namespace std; int main() {    //initialie the array size    int arr[30];    ifstream is("a.txt");    int cnt= 0;    int x;    // check that array is not already full    while (cnt < arr[30] && is >> x)    // and read integer from file    arr[cnt++] = x;    // print the integers stored in the array    cout

How to read file content into istringstream in C++?

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

1K+ Views

Here is a C++ program to read file content into isstringstream in C++.Example#include #include #include using namespace std; int main() {    ifstream is("a.txt", ios::binary );    // get length of file:    is.seekg (0, std::ios::end);    long length = is.tellg();    is.seekg (0, std::ios::beg);    // allocate memory:    char *buffer = new char [length];    // read data as a block:    is.read (buffer,length);    // create string stream of memory contents    istringstream iss( string( buffer ) );    cout

How can I get a file's size in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

13K+ Views

To get a file’s size in C++ first open the file and seek it to the end. tell() will tell us the current position of the stream, which will be the number of bytes in the file.Example Live Demo#include #include using namespace std; int main() {    ifstream in_file("a.txt", ios::binary);    in_file.seekg(0, ios::end);    int file_size = in_file.tellg();    cout

Difference between files written in binary and text mode in C++

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

518 Views

Text modeBinary modeIn text mode various character translations are performed i.e;“\r+\f” is converted into “”In binary mode, such translations are not performed.To write in the files:ofstream ofs (“file.txt”);Orofstream ofs;ofs.open(“file.txt”);to write in the files: ofstream ofs(“file.txt”, ios::binary);orofstream ofs;ofs.open(“file.txt”, ios::binary);To add text at the end of the file:Ofstream ofs(“file.txt”, ios::app);orofstream ofs;ofs.open(“file.txt”, ios::app);To add text at the end of the file:Ofstreamofs(“file.txt”, ios::app|ios::binary);or ofstream ofs;ofs.open(“file.txt”, ios::app|ios::binary);To read files:ifstream in (“file.txt”);orifstreamin ; in.open(“file.txt”);To read files: ifstream in (“file.txt”, ios::binary);orifstream in ;in.open(“file.txt”, ios::binary);Read More

C++ Program to Implement LexicoGraphical_Compare in STL

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

67 Views

The C++ function std::algorithm::lexicographical_compare() tests whether one range is lexicographically less than another or not. A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries.Declarationtemplate bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);AlgorithmBegin    result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())    if (result == true)       Print v1 is less than v2    result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())    if (result == false)       Print v1 is not less than v2 EndExample Live Demo#include #include #include #include using namespace std; int main(void) {    //initialization ... Read More

How to use use an array of pointers (Jagged) in C/C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

514 Views

Let us consider the following example, which uses an array of 3 integers −In CExample Live Demo#include const int MAX = 3; int main () {    int var[] = {10, 100, 200};    int i;    for (i = 0; i < MAX; i++) {       printf("Value of var[%d] = %d", i, var[i] );    }    return 0; }OutputValue of var[0] = 10 Value of var[1] = 100 Value of var[2] = 200In C++Example Live Demo#include using namespace std; const int MAX = 3; int main () {    int var[] = {10, 100, 200};    int i;    for (i = 0; i < MAX; i++) {       cout

C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges

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

315 Views

This is a C++ program in which we generate a undirected random graph for the given edges ‘e’. This algorithm basically implements on a big network and time complexity of this algorithm is O(log(n)).AlgorithmBegin    Function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list.    Initialize i = 0    while(i < e)       edge[i][0] = rand()%N+1       edge[i][1] = rand()%N+1       Increment I;    For i = 0 to N-1       Initialize count = 0       For j = 0 to e-1         ... Read More

C++ Program to Perform Searching Based on Locality of Reference

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

89 Views

Searching based on locality of reference, depends upon the memory access pattern data elements are reallocated.Here linear searching method is used to search an element.AlgorithmBegin    int find(int *intarray, int n, int item)    intialize comparisons = 0    for i = 0 to n-1       Increase comparisons    if(item == intarray[i])       Print element with its index    break    if(i == n-1)       Print element not found    return -1    Print Total comparisons    For j = i till i>0       intarray[j] = intarray[j-1]       intarray[0] = ... Read More

Bind function and placeholders in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see the Bind function and the placeholders in C++. Sometimes we need to manipulate the operation of some functions as we need. We can use some default parameters to get some essence of manipulating.In C++11, one new feature is introduced, called the bind function. This helps us to do such manipulating in some easier fashion. To use these features, we have to use header file.Bind functions with the help of placeholders helps to determine the positions, and number of arguments to modify the function according to desired outputs.Placeholders are namespaces which detect the position of a ... Read More

Shuffle vs random_shuffle in C++

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

368 Views

Here we will see the Shuffle and random_shuffle in C++. Let us see the random_shuffle first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example Live Demo#include using namespace std; int myRandomGenerator(int j) {    return rand() % j; } main() {    srand(unsigned(time(0)));    vector arr;    for (int ... Read More

Advertisements