-4

I am trying to read lines from a file into an array using the fstream and then printing them out. I tried doing this by using a for loop and a getline command, but the program keeps crashing and giving me the "Exception Thrown: write access violation" message. Is there something I should fix in my program or is there a better way to do this?

File Text:

Fred Smith 21
Tuyet Nguyen 18
Joseph  Anderson 23
Annie Nelson 19
Julie Wong 17

Code:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main() {
    cout << "Harrison Dong - 7/21/17" << endl;
    string fileStuffs[4];

    ifstream fin;
    fin.open("C:\\Users\\Administrator\\Documents\\Summer 2017 CIS 118-Intro 
to Comp. Sci\\Module 17\\firstLastAge.txt");
    if (!fin.is_open()) {
        cout << "Failure" << endl;
    }
    for (int i = 0; i < 5 && !fin.eof(); i++) {
        getline(fin, fileStuffs[i]);
        cout << fileStuffs[i] << endl;
    }

    fin.close();
    system("Pause");
    return 0;
}

Thanks!

  • 1
    `string fileStuffs[4];` and `for (int i = 0; i < 5 && !fin.eof(); i++)` array of 4 elements allowed to index to 5. Not a good plan. – user4581301 Nov 23 '17 at 00:35
  • 1
    Also give [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) a read. – user4581301 Nov 23 '17 at 00:35
  • Hi user4581301. I don't think the .eof is the problem because I tried to do it with individual statements instead of a loop (getline(fin, fileStuffs[0]); getline(fin, fileStuffs[1]); etc) and it worked for the first one I wrote but stopped working for two or more getline statements. – Harrison Dongasaurus Nov 23 '17 at 00:49
  • EOF is a side-show here, but something to watch out for. The problem is outlined in the first comment. You're accessing `fileStuffs` out of bounds. – user4581301 Nov 23 '17 at 00:52
  • I get the same problem when I change it to i <= 4 though. – Harrison Dongasaurus Nov 23 '17 at 00:56
  • Max value from < 5: 4. Max value from <= 4: 4. Same thing. `string fileStuffs[4];` is only valid for 0..3. – user4581301 Nov 23 '17 at 00:59
  • Oh haha ok got it. Thanks! – Harrison Dongasaurus Nov 23 '17 at 01:11

1 Answers1

0

is there a better way to do this?

Yes. Use a std::vector.

#include <iostream>
#include <fstream>
#include <string>
#include <vector> // added to get vector

int main() {
    using namespace std; // moved to restrict scope to a place I know is safe.
    cout << "Bill Pratt - 7/21/17" << endl; // changed name
    vector<string> fileStuffs; // vector is a smart array. See documentation link 
                               // above to see how smart

    ifstream fin("C:\\Users\\Administrator\\Documents\\Summer 2017 CIS 118-Intro to Comp. Sci\\Module 17\\firstLastAge.txt");
    if (!fin.is_open()) {
        cout << "Failure" << endl;
    }
    else // added else because why continue if open failed?
    {
        string temp;
        while (getline(fin, temp)) // reads into temporary and tests that read succeeded.
        {
            fileStuffs.push_back(temp); // automatically resizes vector if too many elements
            cout << fileStuffs.back() << endl;
        }
    }
    // fin.close(); file automatically closes on stream destruction
    return 0;
}
user4581301
  • 29,019
  • 5
  • 26
  • 45