Found 7197 Articles for C++

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

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:07:34

15K+ Views

In C++, the file size determines the total number of bytes. To get the file size, we can take reference of the fstream header which contains various functions such as in_file(), seekg(), and tellg(). Example Input: The file name is tp.txt content- "Hello Tutorialspoint" Output: Size of the file is 22 bytes Example Input: The file name is testfile.txt content- "Hello Tutorialspoint" Output: File size is 27 bytes Now, we will demonstrate the C++ programs of the above two examples using file handling. Getting File Size in C++ The short form of fstream header is file stream which ... Read More

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

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

636 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

120 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

620 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

446 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

151 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

548 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

When should we write our own assignment operator in C++?

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

461 Views

Here we will see when we need to create own assignment operator in C++. If a class do not have any pointers, then we do not need to create assignment operator and copy constructor. C++ compiler creates copy constructor and assignment operator for each class. If the operators are not sufficient, then we have to create our own assignment operator.Example Live Demo#include using namespace std; class MyClass { //no user defined assignment operator or copy constructor is present    int *ptr;    public:       MyClass (int x = 0) {          ptr = new int(x);       }    void setValue (int x) {       *ptr = x;    }    void print() {       cout

ctime() Function in C/C++

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

456 Views

The C library function char *ctime(const time_t *timer) returns a string representing the localtime based on the argument timer.The returned string has the following format − Www Mmm dd hh:mm:ss yyyy, where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year.The syntax is like below −char *ctime(const time_t *timer)This function takes the pointer to a time_t, which is containing the calendar time. It returns a string containing date, time info in human readable format.Example Live Demo#include #include int main () {    time_t curtime;    time(&curtime); ... Read More

Advertisements