3

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 using index = 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;
}
Саша
  • 785
  • 2
  • 10
  • And the exact contents of the input file is, what exactly? – Sam Varshavchik Jan 14 '21 at 02:24
  • Assuming the 255 return value is actually from your program's `return 255` part, common causes of file open failure are: (1) program's working directory not the same as the file; (2) file does not exist; (3) file is open for exclusive access in another application (_e.g._ your text editor); (4) user does not have correct permissions to read the file – paddy Jan 14 '21 at 02:24
  • Someone will come along mark this as a dupe with the proper sourcing, but one glaring issue is your while loop expression. Using `.eof()` can and often leads to off-by-one errors. You also have other issues with your code, like your main function having 2 return statements. I highly doubt that you're understanding your requirements correctly. You don't close the file when you're done. And your if check should be negated. It doesn't make a lot of sense to put the entire purpose of your program in a conditional. – sweenish Jan 14 '21 at 02:24
  • @SamVarshavchik Just a list of names/strings – Саша Jan 14 '21 at 02:27
  • 1
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons?) – WhozCraig Jan 14 '21 at 02:28
  • How many strings are in the file? 10, apparently? You've created an array that only stores 9 elements. And then you're _starting_ indexing that array at 3. So you can only read in 6 values before your program hits undefined behavior. Please attach a debugger and step through the program's execution to find the exact place where it terminates. – paddy Jan 14 '21 at 02:28
  • @paddy The file is accessible and it reads the whole file and prints the output as expected but then terminates with an unknown error and windows prompt a window that we are looking for the solution from Microsoft servers. – Саша Jan 14 '21 at 02:30
  • @sweenish The 255 should be only returned if the file is not accessible. – Саша Jan 14 '21 at 02:31
  • @paddy Yes 10 Strings – Саша Jan 14 '21 at 02:31
  • 3
    Yes, and if you attach a debugger, the program will probably break execution and show the call stack _before_ that exception window is shown and your process is terminated by the OS. Now, read the rest of my comment about how your program has guaranteed undefined behavior. – paddy Jan 14 '21 at 02:31
  • What proof can you show that the file exists in the directory the program is executed from? – Sam Varshavchik Jan 14 '21 at 02:34
  • @paddy Yes, The file contains 10 elements/strings and I am trying to read only 7 strings starting from 3 to 10 – Саша Jan 14 '21 at 02:34
  • @SamVarshavchik The program prints all the list of strings as expected and then return 255, its mean the file was accessible and now not accessible + windows exception of checking for the solution. – Саша Jan 14 '21 at 02:35
  • Your loop reads all strings in the file. And possibly attempts another, due to improper use of the EOF test. Your array holds only 9 values, and the 10th string will be stored at index 12. Is there something you still don't understand? I've kinda said it 3 times now, and you're still responding as if you haven't taken in what I'm saying. – paddy Jan 14 '21 at 02:35
  • @paddy Yes It reads all the strings from start to end but I am forcing it to read from the third string and it reads + at the same time it shows the file is not accessible returning 255 – Саша Jan 14 '21 at 02:38
  • 1
    UNDEFINED BEHAVIOR – paddy Jan 14 '21 at 02:39
  • 2
    Look, try this: set `DataCounterArray` to a value of 20 and run your program. – paddy Jan 14 '21 at 02:40
  • @paddy Yes its now working fine, and I understand what the problem was ) – Саша Jan 14 '21 at 02:45

0 Answers0