【发布时间】:2012-05-30 19:19:29
【问题描述】:
首先,我对 c++ 的了解非常有限,这是我的第一堂课,所以这可能看起来很愚蠢,但请耐心等待,我想请你保持简单或解释清楚,提前谢谢。
好的,我的目标是创建这个包含规则的文件,以便我可以在单独的函数中读取它。但是,当我运行它时,我没有收到任何错误,但它首先出现时根本没有空格,最后我得到一些随机的 ascii。
这是运行代码的结果 欢迎来到打字游戏1.在一行中键入整个句子2.Istimed and willaffectthescoreØ-™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;
}
【问题讨论】:
标签: c++ arrays char 2d fstream