0

I am reading from a file into a 2-D array in c++. I used a do while loop together with the eof(). The problem I was facing was, the program continues to read empty spaces when the characters in the file are exhausted. I realized that it was because of the getline function, so I used the get function instead. The program compiles and runs successfully but upon reaching the point where the characters in the file is to be read,(i.e in my console), the programs stops suddenly and returns with this error code -1073741795 (0xc00000ID). I would like to know why though, my major concern is how I will be able to read from the file and after the characters in the file is exhausted, the program stops reading from the file (so that no empty spaces are read). Here is the sample code:

    int k=0;
do{
    name_input.getline(sample[k],num_char);
k++;
} while (!name_input.eof());
num=k-1;

PS: The input file is in the form; Mr. Clark Smith John B. Doe Joshua Clement Johnson each on one line in a .txt file (notepad), after the 3rd name is empty space which my program still reads.

3NaNa7
  • 51
  • 1
  • 7
  • while ( name_input.getline() ) { } would be better – drescherjm Jul 20 '20 at 20:05
  • ***I used a do while loop together with the eof()*** [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Jul 20 '20 at 20:05
  • What is `sample` ? Maybe you should use a std::vector if it's a fixed size array? Maybe you should verify that `k` is less than the size – drescherjm Jul 20 '20 at 20:06
  • I think if you need more help than has been provided you need to provide a better [mcve] your current code leaves out a lot of details. – drescherjm Jul 20 '20 at 20:15
  • @drescherjm ```sample``` is the name of the 2-Array containing the list of names read from the input file. The ```k``` iterating to provide the indexing. The column wasn't necessary, that is why I didn't bring it. – 3NaNa7 Jul 21 '20 at 16:07
  • Maybe you exceed the bounds of your array with `k`. Maybe the line read is longer than the width of the array. – drescherjm Jul 21 '20 at 16:41

1 Answers1

0

Using eof() is almost always wrong (see link in comments above). This is how to write the loop

int k = 0;
while (name_input.getline(sample[k],num_char)) {
    k++;
}
john
  • 85,011
  • 4
  • 57
  • 81
  • Please @john, when I tried your method. My program stopped suddenly when the program control reached where the names are read. ```Process terminated with status -1073741795 (2 minute(s), 55 second(s)) ``` – 3NaNa7 Jul 21 '20 at 16:17
  • @Samuelson No one can help you with that since the bug that causes the crash is not in the code that is shown or its not obvious from the limited code shown. – drescherjm Jul 21 '20 at 16:39
  • 1
    @Samuelson Unfortunately it's often not possible to fix a bug without having a complete program. Programs crash in one place but the bug is actually in some code that executed earlier. – john Jul 21 '20 at 18:30
  • Thanks guys, I was able to figure out what the problem is. I pressed the return key several times when the list of names in my file finished. Since getline reads whitespaces, it read these too. Thanks once again – 3NaNa7 Jul 27 '20 at 17:11