首先,我对 C++ 的了解非常有限,这是我的第一堂课,所以这可能看起来很愚蠢,但请耐心等待,我会要求您保持简单或解释清楚,提前谢谢。
好的,目标是我想用里面的规则创建这个文件,这样我就可以在一个单独的函数中读取它。但是,当我运行它时,我没有收到任何错误,但它首先出现时根本没有空格,最后我得到了一些随机的 ascii。
这是运行代码的结果 Welcometothetypinggame1.Typetheentiresentenceinoneline2.IstimedandwillaffectthescoreØ-™wîú{ Dx&a¼ Ë' ©ÉaDx&a®pa
#include <cstdlib>
#include <fstream>
#include <iostream>
int main(int argc, char** argv) {
//set the sentences i want the file to print
char o[3][40]={"Welcome to the typing game ",
"1. Type the entire sentence in one line",
"2. Is timed and will affect the score "};
//creating the file to read in
ofstream out;
out.open("rules.txt");
for(int i=0;i<3;i++){
for(int x=0; x<39; x++){
out<<o[i][x];
}
out<<"\n";
}
out.close();
//creating a new array to read the data that was stored above
char a[3][40];
ifstream in;
in.open("rules.txt");
for(int i=0;i<3;i++){
for(int x=0; x<40; x++){
in>>a[i][x];
}
}
in.close();
//and just printing out the array to see the results
for(int i=0;i<3;i++){
for(int x=0; x<40; x++){
cout<<a[i][x];
}
}
return 0;
}