Here is the answer for your question
in C++ Programming language.
What this code
does?
1. Here I have given you a code that
reads a file "student.txt" which contains columns
as you specified.
2.Stores the surname column in a
vector (as you mentioned size can be anything I used vectors)
3. Sorts the surnames column in
alphabetical order and returns the vector.
4. Searches for every surname from
starting in the vector with the lines in the file from starting and
if founds any match displays the particular line of the file.
5. Hence the names which are in
alphabetical order in the vector are used to display their lines
associated with them.
#############################################################################################
CODE:(cpp file)
//Including required header files
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include <bits/stdc++.h>
//using standard namespace
using namespace std;
//function that compares the alphabetical order of the
string
bool compare(string word1,string word2)
{
return word1<word2;
}
//method that starts sorting and takes each word in the vector as
input
vector<string> alphasort(vector<string> word)
{
int n = word.size();
//sort method that actually does sorting
sort(word.begin(),word.end(),compare);
//returning the particular word
return word;
}
//main method where compilation starts
int main()
{
//String variable to store data of each column in text
file except surname
//These are used for just storage while reading file
and we no more use them again in the program
string sno , first , middle , bday , course , year
,schl_year;
//Vector to store the surnames column in the
file
vector<string> surnames;
//variables to store lines of the file and to store
the surnames
string line , name;
//Counters are used as eof() function reads last line
twice
//To avod that counters are used counter 1 counts
number of lines
//Counter 2 takes the loop to repeat through the same
number of lines
int counter1 =0 , counter2 = 0;
size_t position;
//Object of ifstream
ifstream Input ;
//filename of the string
string filename = "birthday.txt";
//Opening the file
Input.open(filename.c_str());
//While lop to calculate number of lines in the
file
while(getline(Input,line))
{
counter1++;
}
//After while loop as the pointer lies at end we have
to close and reopen the file to start from beginning again
Input.close();
Input.open(filename.c_str());
//Second while loop that iterates through each word of
the file
while(!Input.eof())
{
//Each word in every line is being
stored in the respected variable name
Input >> sno >> name
>> first >> middle >> bday >> course
>> year >> schl_year;
//Pushig=ng only surnames to vector
as they are only to be sorted
surnames.push_back(name);
//To avoid while loop to repeat
last line twice
if(counter2==(counter1-1))
{
break;
}
counter2++;
}
Input.close();
Input.open(filename.c_str());
//calling alphasort function ans replacing the
original vector with sorted vector
surnames = alphasort(surnames);
//Printing statements for understanding
cout << "StudentNo " << " Surname "
<<" FirstName " <<" Middle " <<" BDay " <<"
Course " << " Year " <<" School "<< endl;
cout <<
"---------------------------------------------------------------------------------------"<<endl;
//loop that iterates throuugh the size of the
vector
for(int i =0;i<surnames.size();i++)
{
//At each iteration it takes
current surname in vector
//while loop to iterate through the
lines of file and reading every line each time in string line
while(getline(Input,line))
{
//The current
surname is checked to be in currnet line of the file
position =
line.find(surnames[i]);
//if yes prints
the line
if(position!=string::npos)
{
cout << line << endl;
}
//if not checks
the next line of the file
}
//For every while loop iteration
the file needs to be closed and opened as every iteration ends at
the end of the file
Input.close();
Input.open(filename.c_str());
}
return 0;
}
|
student.txt
1 Johnes Fedrick Richard 06
CSE 2019 2013
2 Nicholas Rathon Freck 18 EEE 2018 2012
3 Coprus Jackson Rone 23 ECE 2016 2010
4 Hindle Ackson John 21 CSE 1997 1991
5 James Rathod Zafer 30 EEE 1998 1992
6 Hadrick Nelson Koman 15 CSE 2000 1994 |
SCREENSHOTS
Please see the screenshots for the indentations of the code.



If below is the text file (Example values taken )

Then the OUTPUT will be (sorting surnames in
alphabetical order)

If you want to store the lines in the same order you can store
it in another file and use them for further process.