Chase Convery Chase Convery - 8 days ago 6
C++ Question

cannot convert FILE* -C++

I've been asked to make a program for my university course and it needs to check how many open and closed brackets are in a file. I am getting two error messages for my function:


40:22: cannot convert FILE* {aka_IO_FILE*} to 'const char*' for argument '1' FILE*

fpin = fopen(fpin);

42:22: warning format '%s' expects argument of type 'char*', but argument 2 has type '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);
}

Answer

Replace the argument fpin here with the file you want to open. Which you probably want to be arr. You'll also need to pass a mode argument. Just "r" would be appropriate here.

fpin = fopen(fpin);

Replace arr[1] with just arr. arr[1] would be the second character of arr and not the string %s requires.

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

This should be enough for it to compile..

Comments