I am trying to read from a specific line/block using array
manipulation of index, but the program is terminating with an unknown error after reading from the third line to end, and returning 255
, How to achieve it using the same method I implemented in the code if this is possible?
Create a file with 10 blocks and name it
datafile.txt
then try to read the file starting from the third block usingindex = 3
it will read from the the whole file starting from the 3rd line and then terminates with 255 telling file not accessible.The expected output is it should start reading from the third block to the end of the file
eof
and prints the output without any error.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream datafile("datafile.txt");
if(datafile)
{
cout << "The file is accessible to read" << '\n';
const int DataCounterArray = 9;
string nameList[DataCounterArray] = {""};
string getDataTemporary = {""};
int index = 3;
while (!datafile.eof())
{
nameList[index] = getDataTemporary;
index++;
datafile >> getDataTemporary;
cout << getDataTemporary << '\n';
}
for (int i=0; i < DataCounterArray; i++)
{
cout << nameList[i] << '\n';
}
}
else{
cout << "The file is not accessible to read" << '\n';
return 255;
}
return 0;
}