首先我對C++的瞭解非常有限,這是我的第一堂課,所以這可能看起來很愚蠢,但對我忍無可忍,我會問你保持它有點簡單還是很好解釋提前致謝。從C++文件中讀取二維字符數組
好吧,我的目標是我想用裏面的規則創建這個文件,這樣我就可以在一個單獨的函數中讀取它。但是,當我運行這個我沒有得到任何錯誤,但它首先沒有空格,最後我得到了一些隨機的ascii。
這裏是運行代碼 Welcometothetypinggame1.Typetheentiresentenceinoneline2.IstimedandwillaffectthescoreØ-™WIU {DX &那張E」©É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;
}
感謝您快速解釋的答案 –