My first thought was the code looks more complex than it needs to be. but that was a gut feeling, rather than a proper analysis.
Anyway, the main problem is the size of the buffer.
This is required to hold these characters:
'-', '1', '0', '0' - this makes four
in addition the char '\t' is tacked onto the end, that makes five. Properly there should also be a null terminator, which makes a total of 6.
So, increase the buffer size to 6, and add the null terminator '\0' after line 56.
Alternative version, based upon the above code, but slightly modified:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
int main()
{
string line;
string word;
string array[3][8];
ifstream myfile("..\\..\\data.txt");
if (myfile.is_open())
cout << "File Open\n";
else
cout << "File not open\n";
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 8; ++j)
{
if (myfile>>word) {
if (word[1] == '=')
{
array[i][j] = word.substr(2);
cout << array[i][j] << ' ' ;
}
}
}
cout << endl;
}
myfile.close();
cout<<endl<<"love"<< endl;
cin.get();
return 0;
}
|