Head & Tail programs
Sample program output for Q12-1: File Head Program
Example 1:
Enter the file name: 5line.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
The entire file has been displayed.
Example 2:
Enter the file name: 20line.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
Sample program output for Q12-4: File Tail Program
Example 1:
Enter the file name: 5line.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
The entire file has been displayed.
Example 2:
Enter the file name: 20line.txt
This is line 12
This is line 13
This is line 14
This is line 15
This is line 16
This is line 17
This is line 18
This is line 19
This is line 20
Programming Assignment Report
For each programming assignment, you need to answer the following:
- Your Name, Student ID, assignment number and the program title.
- Discussion: Complete the DISCUSSION section. It does not need to be long, but it needs to be complete.
- What did you do to develop the program? (“Followed the Directions” is not a complete description)
- What problems did you have and how did you overcome the problems?
- PROGRAM OUTPUT: Show the necessary screen shots of your program outputs in sequence. Note that it is considered “cheating” if your program cannot re-produce the results shown on your screenshots.
Solution
FileHead.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
stringfileName;
cout<< “Enter the file name: “;
cin>>fileName;
fstream file(fileName.c_str());
vector<string> lines;
string line;
while (getline(file, line))
{
lines.push_back(line);
}
int i = 0;
while (i < 10 && i <lines.size())
{
cout<< lines[i] <<endl;
i++;
}
cout<<endl<<endl<< “The entire file has beed displayed”;
}
FileTail.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
stringfileName;
cout<< “Enter the file name: “;
cin>>fileName;
fstream file(fileName.c_str());
vector<string> lines;
string line;
while (getline(file, line))
{
lines.push_back(line);
}
int i;
if(lines.size() < 10)
i = 0;
else i = 10;
while (i <lines.size())
{
cout<< lines[i] <<endl;
i++;
}
cout<<endl<<endl<< “The entire file has beed displayed”;
}