我試圖爲不同的級別打開不同的文件,並且需要一個變量名才能這樣做。我嘗試以下,但給了錯誤: 「從字符串不適合轉換爲const char」讀取帶有變量名稱的文件C++
void loadMap(){
//string levelname;
//levelname = '../Levels/Level' + level;
FILE *file;
file = fopen("../Levels/Level" + level + ".txt", "r"); //THIS LINE IS GIVING THE ERROR
char section[80];
int index = 0;
int x = 0;
int y = 0;
while(true){
fscanf(file, "%s", section);
if(strcmp(section, "[Victor]") == 0){
while(true){
fscanf(file, "%d%d%d", &index, &x, &y);
if(index == -1){
break;
}
victor.x = x;
victor.y = y;
}
}
... ... //更多代碼
儘管此代碼可以回答這個問題,提供有關附加的上下文*爲什麼*和/或*該代碼如何回答這個問題可以提高其長期價值。 –