2016-11-25 83 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