C++ sorting program: Operation of this C++ example source code is used to open a text file, read the content and sort in a particular order. These kinds of file operations related programs are very important for academics. It is easy to follow and there is the simple code. This object oriented C++ programming will do the following functions while compiling the code.
- Open a Text format file,
- Display text file’s content,
- Content sorting,
- Display sorted content.
- Read: Call by reference programs
All above operations are happening based on the C++ sorting functionality. This C++ source code can be used as a LAB exercises program or exam program. All students can use this important program for free of cost. Feel free to share about us to the world.
- Related: C program Examples & Explanation
C++ Sort program:
Write a C++ code for text file content sorting with the use of strcpy() method.
// Header files section #include<iostream.h> #include<string.h> #include<conio.h> #include<fstream.h> void main() { clrscr(); // Removes the previous output screen char k[30],s[35][30],x[30]; //Variables declaration int v,C1; v=C1=0; ifstream ifs("C:\\karthikh-venkat.txt"); ofstream sort("C:\\SORTED-TEXT.txt"); cout<<"\n\t Contents Before Sorting Operation"; while(!ifs.eof()) { ifs.getline(k,30); // Reading Text file strcpy(s[C1],k); cout<<k<<"\n"; if(C1) { // Sorting operations takes place strcpy(s[C1],k); for(v=0;v<=C1;v++) { if(strcmp(s[C1],s[v])<0) { strcpy(x,s[v]); strcpy(s[v],s[C1]); strcpy(s[C1],x); } } } C1++; } cout<<"\n Display After Sorting"; for(v=0;v<C1;v++) { sort<<s[v]; cout<<"\n"<<s[v]; // Final output display } getch(); }