我被要求爲我的大學課程製作程序,它需要檢查文件中有多少個開放和封閉的括號。我正在爲我的函數的兩個錯誤信息:無法轉換文件* -C++
40:22:不能轉換FILE * {aka_IO_FILE *}以 '爲const char *' 的說法 '1' FILE *
fpin =的fopen(fpin);
42:22:警告格式'%s'需要類型'char *'的參數,但參數2的類型爲'int'
printf(「Could not open%s \ n」,arr [1]);
void countBrackets(char arr[])
{
int bracketCount = 0:
int lineNumber = 0;
//tracking position in file
FILE* fpin;
//open file and check to make sure
//file opened safley
fpin = fopen(fpin);
if (fpin == NULL){
printf("Could not open %s \n", arr[1]);
return;
}
//Count how many opened and closed { and } brackets are
//in the file
for (char currCh = 0; currCh > lineNumber; currCh ++){
if (currCh == '{') {
bracketCount ++;
}else if (currCh == '}') {
bracketCount ++;
}else (currCh == '\n') {
lineNumber ++;
}
//display error messages for the user
if (bracketCount < 0) {
printf("there is more closing brackets than opening\n");
}else{
printf("There is more opening brackets than closing\n");
}
}
fclose(fpin);
}
閱讀'的fopen()'庫函數的說明。它的第一個參數必須是一個'const char *'。由於'arr'是一個數組,'arr [1]'是一個'char','%s'格式規範需要'const char *'值。 –