0
vector<string> arr;

int size = 0;
fstream buffer;
ifstream w;
buffer.open("test.txt");

if (buffer.fail()) {
  cout << "invalid";
  return;
}

string g;

getline(buffer, g, '\n');
setPlane(g);

while (!buffer.eof()) {
  getline(buffer, g, '\n');
  size += 1;
arr.resize(size);
  arr[size - 1] = g;
}

buffer.close();
w.open("test.txt");
for (int i = 0; i < size; i++) {
  w << arr[i] << '\n';
}
w.close();

I'm having an issue where if specifically the ofstream and ifstream are linked to the same file, the program crashes upon running a second a time, but crashes upon running a second time with the updated file.

  • Does this answer your question? [Why does my program crash upon writing to a file?](https://stackoverflow.com/questions/40432898/why-does-my-program-crash-upon-writing-to-a-file) – kaustubhd9 Jun 14 '20 at 06:37
  • 3
    `arr` is initialized with a size of `0` and never resized in the show code. So `arr[size - 1]` results into an out of bounds access an therefore in undefined behavior. – t.niese Jun 14 '20 at 06:50
  • Does this answer your question? [Vector assignment crashing](https://stackoverflow.com/questions/40552250/vector-assignment-crashing) – t.niese Jun 14 '20 at 06:51
  • hi i edited the code, now its a different error – iceman harden Jun 14 '20 at 06:55
  • What does `setPlane()` do? – G. Sliepen Jun 14 '20 at 06:59
  • 2
    Some unrelated hints: [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). And `arr.resize(size); arr[size - 1] = g;` is the same as `arr.push_back(g);` – churill Jun 14 '20 at 07:07
  • What error do you get with the updated code? – Alan Birtles Jun 14 '20 at 07:17
  • the compiler doesn't say, the code just ends as soon as the first readline happens – iceman harden Jun 14 '20 at 07:23
  • Oh setplane is a function that takes the string and saves it another object. – iceman harden Jun 14 '20 at 07:23
  • 2
    Can you please make a [mre]? Maybe the error is in, or caused by, `setPlane`. Also it seems like a good time to [learn how to use a debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – churill Jun 14 '20 at 07:36
  • `the compiler doesn't say` not getting any errors does only mean that the code has the correct syntax, but not that it is valid. Warnings will indicate that something in your code is wrong that could result in unexpected behavior at runtime. But even without warnings, it does not mean that your code is correct. And the place at which the application crashes does not need to be the place that is causing the crash, your application could already be in an undefined state due to an early undefined behavior. – t.niese Jun 14 '20 at 07:43
  • 1
    Your sample code doesn't compile. Is w meant to be an ofstream? – digby280 Jun 14 '20 at 09:19
  • @icemanharden Replace `ifstream` with `ofstream`, and try then as your code doesn't compile as-is. Otherwise, run your debugger; even just running a program using a debugger will tell you the exact line the crash occurs. – Guy Keogh Jun 14 '20 at 13:27

0 Answers0