-
fopen() and open()
hi all,
i have a question,
whats the big difference between fopen() and open()?
what are the pro's and con's of both functions,
because you can open/wite/read files with both of them..
I find open() slightly harder to use then fopen().
But is it that open is UNIX/Linux only and fopen works on both?
thanks in advance
enrypted
-
Function fopen() opens a file and returns a pointer to FILE. You need to include stdio.h for it.
FILE *fopen (const char * restrict filename, const char * restrict mode);
Function open() is not a standard C function. It is a system call to open a file or device. To use it, you must include fcntl.h. But since it is not a standard C function, it may not be available in your system. Unix-like systems usually have it.
int open(const char *path, int flags, ...);
The pro of using fopen() is that it is standard.
-
Ok, thanks
so the bottom line is; fopen() is standard
open() is a UNIX system call
encrypted
-
open has a lot of extra features. Check that man page ;) (only if you're able to use it of course)
-
Ok thanks,
but i have a problem now:
Code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int fd1, fd2;
char *buffer;
fd1 = open( "dude.dat", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR );
fd2 = open( "dude.dat", O_RDONLY );
write( fd1, "UNIX", 5 );
read( fd2, buffer, 5 );
printf("%s\n", buffer );
close( fd1 );
close( fd2 );
exit(0);
}
If i compile this code ( with gcc ) and run it, i dont get 'UNIX' on my screen but some weird characters that i've never seen before
:confused:
Anyone know what im doing wrong?
thank you
encrypted
-
First, always perform error-checks. Check if the value that function open() returns is no error-value. In case open() returns an error-value, you cannot use that file-descriptor.
Second, you try open the same file twice. Try an algorithm like this:
- open the file for writing
- write to it
- close the file
- open the file for reading
- read from it
- close the file
-
Thanks man :)
i got it working,
and btw is this what you suggested by error checking? :
Code:
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
char buffer[5];
if( ( fd = open( "data.dat", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR ) ) != -1 )
{
write( fd, "UNIX", 5 );
}
else
{
printf("File could not be opened");
exit(1);
}
close( fd );
if( ( fd = open( "data.dat", O_RDONLY ) ) != -1 )
{
read( fd, buffer, 5 );
}
else
{
printf("File could not be read");
exit(1);
}
close( fd );
printf("Result read from file:\n %s\n", buffer );
exit(0);
}
encrypted
-
Yes that is what I meant with error-checking. A lot of standard functions return a specific value when an error occurred, checking the return values and take the right action when an error occurred results in more robust programs. When you don't perform error-checking, it is the same as assuming everything will always go right, which may result in undesirable behaviour of the code. Using error-checking you correct such behaviour.
[edit]
BTW, using function exit() is not necessary here. You could just use return 0 and return 1.
[/edit]
-
>>read( fd, buffer, 5 );
Unlike fgets(), read() won't terminate the array with a nul byte, so doing this might produce strange results:
>>printf("Result read from file:\n %s\n", buffer );
If you want to print the array as a string, make sure its nul terminated first.