In: Computer Science
Done in c++
four score and seven years ago our fathers brought forth on
this continent a new earth |
The file that you should read:
four score and seven years ago our fathers brought forth on this continent a new nation conceived in liberty and dedicated to the proposition that all men are created equal now we are engaged in a great civil war testing whether that nation or any nation so conceived and so dedicated can long endure we are met on a great battlefield of that war we have come to dedicate a portion of that field as a final resting place for those who here gave their lives that that nation might live it is altogether fitting and proper that we should do this but in a larger sense we can not dedicate we can not consecrate we can not hallow this ground the brave men living and dead who struggled here have consecrated it far above our poor power to add or detract the world will little note nor long remember what we say here but it can never forget what they did here it is for us the living rather to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced it is rather for us to be here dedicated to the great task remaining before us that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion that we here highly resolve that these dead shall not have died in vain that this nation under god shall have a new birth of freedom and that government of the people by the people for the people shall not perish from the earth
Source Code:
#include<iostream>
#include<fstream>
using namespace std;
int main () {
//Step 1:
ifstream file("InputFile.txt");//opening input file first time. Note: change the input file name as you want.
if(!file.is_open())//Checking whether the file is opened correctly or not.
{
cout<<"Error opening File."<<endl;
exit(1);
}
string str;
int lineCount=0;//variable to keep the count of number of lines in the file.
while(getline(file, str))//reading each line with str variable until end of file is reached.
{
lineCount++;// incrementing lineCount while reading each line at a time.
}
file.close();//closing the file.
//Step 2:
string *fileData=new string[lineCount]; // dynamically allocating memory of string array of the size of lineCount.
//Step 3:
ifstream file1("InputFile.txt");//opening input file second time. Note: change the input file name as you want.
if(!file1.is_open())
{
cout<<"Error opening File."<<endl;//gives error message when the file is not present.
exit(1);
}
int index=0; //index to store the current line.
while(getline(file1,str) && index<lineCount)// read each line from the file until reaches the end of file and also checking the index whether it exceeds the line lineCount.
{
fileData[index]=str; // storing the current line to the fileData at the position of index.
index++;//incrementing index to point the next memory location.
str.clear();// clearing str data.
}
file1.close();//closing the file.
//Step 4:
cout<<fileData[0]<<endl;// printing first line.
cout<<fileData[lineCount-1]<<endl;// printing last line.
//Step 5:
delete[] fileData;//deallocating the fileData to freeup space.
return 0;
}
Input File Data:
Output: