I have file with this code:
start:
var: a , b , c;
a = 4;
b = 2;
c = a + b;
wuw c;
end;/
I created a class that contains an array of characters in which my code resides:
class file{ //class of program file
private:
ifstream File; //file
char text[X][Y]; //code from file
Information from file I load to array using constructor of class:
file(string path)
{
File.open(path); //open file
for(int x = 0 ; x < X ; x++)
{
for (int y = 0; y < Y ; y++) text[x][y] = File.get();
}
}
In class I have function that write to console text from array:
void write()
{
for (int x = 0 ; x < X ; x++)
{
for (int y = 0 ; y < Y ; y++) cout << text[x][y];
}
}
But after calling write() function I have this text:
start:
var: a , b , c;
a = 4;
b = 2;
c = a + b;
wuw c;
end;/
������������
����������������������������������������
����������������������������������������
����������������������������������������
����������������������������������������
����������������������������������������
The size of text
does not correspond to the size of the file. That's not only wasteful, but in this case it's causing you to read off the end of the file. A better design would be to instead define vector<string> text
. Using a valid ifstream File
you could populate this text
in the body of your constructor like this:
for(string i; getline(File, i); text.push_back(i));
From there, you'd also need to adapt write
to:
copy(cbegin(text), cend(text), ostream_iterator<string>(cout, "\n"));
You'd also need to put in safeguard checks that the indices passed into no_zero
and ret_char
were valid, but the rest of the code should work as is.