'�' 而不是文件中的普通文本
'�' instead normal text from file
我有包含此代码的文件:
start:
var: a , b , c;
a = 4;
b = 2;
c = a + b;
wuw c;
end;/
我创建了一个 class,其中包含我的代码所在的字符数组:
class file{ //class of program file
private:
ifstream File; //file
char text[X][Y]; //code from file
我使用 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();
}
}
在 class 中,我有从数组写入控制台文本的功能:
void write()
{
for (int x = 0 ; x < X ; x++)
{
for (int y = 0 ; y < Y ; y++) cout << text[x][y];
}
}
但是在调用 write() 函数后我得到了这个文本:
start:
var: a , b , c;
a = 4;
b = 2;
c = a + b;
wuw c;
end;/
������������
����������������������������������������
����������������������������������������
����������������������������������������
����������������������������������������
����������������������������������������
text
的大小与文件大小不符。这不仅浪费,而且在这种情况下还会导致您读取文件末尾。更好的设计是改为定义 vector<string> text
。使用有效的 ifstream File
,您可以像这样在构造函数的主体中填充此 text
:
for(string i; getline(File, i); text.push_back(i));
从那里开始,您还需要将 write
调整为:
copy(cbegin(text), cend(text), ostream_iterator<string>(cout, "\n"));
您还需要进行安全检查以确保传递给 no_zero
和 ret_char
的索引有效,但其余代码应按原样工作。
我有包含此代码的文件:
start:
var: a , b , c;
a = 4;
b = 2;
c = a + b;
wuw c;
end;/
我创建了一个 class,其中包含我的代码所在的字符数组:
class file{ //class of program file
private:
ifstream File; //file
char text[X][Y]; //code from file
我使用 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();
}
}
在 class 中,我有从数组写入控制台文本的功能:
void write()
{
for (int x = 0 ; x < X ; x++)
{
for (int y = 0 ; y < Y ; y++) cout << text[x][y];
}
}
但是在调用 write() 函数后我得到了这个文本:
start:
var: a , b , c;
a = 4;
b = 2;
c = a + b;
wuw c;
end;/
������������
����������������������������������������
����������������������������������������
����������������������������������������
����������������������������������������
����������������������������������������
text
的大小与文件大小不符。这不仅浪费,而且在这种情况下还会导致您读取文件末尾。更好的设计是改为定义 vector<string> text
。使用有效的 ifstream File
,您可以像这样在构造函数的主体中填充此 text
:
for(string i; getline(File, i); text.push_back(i));
从那里开始,您还需要将 write
调整为:
copy(cbegin(text), cend(text), ostream_iterator<string>(cout, "\n"));
您还需要进行安全检查以确保传递给 no_zero
和 ret_char
的索引有效,但其余代码应按原样工作。