Greetings,
First timer posting so please forgive some of the code posting prettiness I haven't figured out yet...
Running FreeBSD since the old Dr.Dobbs articles and just now trying to get back into some C programing and some other scripting. Learned K&R C back in 1986 or so and did data acquisition in the 90s. Basicly been a JOAT for the last 20 years and am trying to get back into programming and ran across this little gem of a problem.
System - FreeBSD on MS HyperV and VMWare Player - FreeBSD version 14.3 Release P7.
So, my problem is with fopen() and fclose(); man 3 says that fopen() returns a FILE * and fclose() requires FILE * parameter.
This is not correct. Function fopen() returns type int while function fclose() requires parameter FILE *.
So, WHAT IS UP with this?
Here is the screen copy of code and compiler -- cc -- results
file handle = FILE *
LISTING 1 ----------
root$ cat test.c
#include <stdio.h>
#include <stdlib.h>
/*-------------------------------------------*/
int main(int argc, char *argv[])
{
FILE *fptr;
if (argc != 2)
{
printf("Error: test filename \n");
exit(-1);
}
printf("%s\n",argv[1]);
if ((fptr = fopen(argv[1],"r+") == NULL))
{
printf("test: %s: No such file\n", argv[1]);
exit(-1);
}
fclose(fptr);
exit(0);
}
root$ cc test.c
test.c:14:13: error: incompatible integer to pointer conversion assigning to 'FILE *' (aka 'struct __sFILE *') from 'int' [-Wint-conversion]
14 | if ((fptr = fopen(argv[1],"r+") == NULL))
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
root$
file handle = int
LISTING 2 -----------------------
root$ cat test.c
#include <stdio.h>
#include <stdlib.h>
/*-------------------------------------------*/
int main(int argc, char *argv[])
{
int fptr;
if (argc != 2)
{
printf("Error: test filename \n");
exit(-1);
}
printf("%s\n",argv[1]);
if ((fptr = fopen(argv[1],"r+") == NULL))
{
printf("test: %s: No such file\n", argv[1]);
exit(-1);
}
fclose(fptr);
exit(0);
}
root$ cc test.c
test.c:19:10: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'FILE *' (aka 'struct __sFILE *') [-Wint-conversion]
19 | fclose(fptr);
| ^~~~
/usr/include/stdio.h:251:19: note: passing argument to parameter here
251 | int fclose(FILE *);
| ^
1 error generated.
root$
END-
First timer posting so please forgive some of the code posting prettiness I haven't figured out yet...
Running FreeBSD since the old Dr.Dobbs articles and just now trying to get back into some C programing and some other scripting. Learned K&R C back in 1986 or so and did data acquisition in the 90s. Basicly been a JOAT for the last 20 years and am trying to get back into programming and ran across this little gem of a problem.
System - FreeBSD on MS HyperV and VMWare Player - FreeBSD version 14.3 Release P7.
So, my problem is with fopen() and fclose(); man 3 says that fopen() returns a FILE * and fclose() requires FILE * parameter.
This is not correct. Function fopen() returns type int while function fclose() requires parameter FILE *.
So, WHAT IS UP with this?
Here is the screen copy of code and compiler -- cc -- results
file handle = FILE *
LISTING 1 ----------
root$ cat test.c
#include <stdio.h>
#include <stdlib.h>
/*-------------------------------------------*/
int main(int argc, char *argv[])
{
FILE *fptr;
if (argc != 2)
{
printf("Error: test filename \n");
exit(-1);
}
printf("%s\n",argv[1]);
if ((fptr = fopen(argv[1],"r+") == NULL))
{
printf("test: %s: No such file\n", argv[1]);
exit(-1);
}
fclose(fptr);
exit(0);
}
root$ cc test.c
test.c:14:13: error: incompatible integer to pointer conversion assigning to 'FILE *' (aka 'struct __sFILE *') from 'int' [-Wint-conversion]
14 | if ((fptr = fopen(argv[1],"r+") == NULL))
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
root$
file handle = int
LISTING 2 -----------------------
root$ cat test.c
#include <stdio.h>
#include <stdlib.h>
/*-------------------------------------------*/
int main(int argc, char *argv[])
{
int fptr;
if (argc != 2)
{
printf("Error: test filename \n");
exit(-1);
}
printf("%s\n",argv[1]);
if ((fptr = fopen(argv[1],"r+") == NULL))
{
printf("test: %s: No such file\n", argv[1]);
exit(-1);
}
fclose(fptr);
exit(0);
}
root$ cc test.c
test.c:19:10: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'FILE *' (aka 'struct __sFILE *') [-Wint-conversion]
19 | fclose(fptr);
| ^~~~
/usr/include/stdio.h:251:19: note: passing argument to parameter here
251 | int fclose(FILE *);
| ^
1 error generated.
root$
END-