2016-11-25 45 views
-4

我被要求爲我的大學課程製作程序,它需要檢查文件中有多少個開放和封閉的括號。我正在爲我的函數的兩個錯誤信息:無法轉換文件* -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); 
} 
+4

閱讀'的fopen()'庫函數的說明。它的第一個參數必須是一個'const char *'。由於'arr'是一個數組,'arr [1]'是一個'char','%s'格式規範需要'const char *'值。 –

回答

0

你想打開的文件替換參數fpin這裏。你可能想要成爲arr。你還需要傳遞一個模式參數。只需要"r"就可以了。

fpin = fopen(fpin); 

更換arr[1]arrarr[1]將是arr的第二個字符,而不是%s所要求的字符串。

printf("Could not open %s \n", arr[1]); 

這應該足以爲它來編譯..

+2

這個答案沒有解決OP代碼的許多問題 – Tas