[LCP]creating files with open()
Biki
biki at deeproot.co.in
Mon Oct 1 15:12:48 UTC 2001
On Sat, 29 Sep 2001, Andy Zivkovic spat out
>--<Hi,
>--<
>--<I'm trying to open/create a file if it doesn't exist, then write to it,
>--<however writes are failing. Additionally, the permissions on the file is
>--<really strange. On linux (home) and windows (home) the function I've copied
>--<below creates the file, but the write fails, and the close succeeds. On
>--<Solaris (uni) the open fails.
>--<
>--<Is there anything immediately obvious with my program?
>--<
>--<The test program I'm using is:
>--<
>--<int main(int argc, char *argv[]) {
>--< int file, ret;
>--<
give a file pointer, do not declare the file as an integer
FILE *file
>--< file = open("file.txt", O_CREAT|O_EXCL);
>--<
to open the file and to write to it open it in the write mode
for example :
file = fopen("file.txt","w");
for other options go thru the man pages
type 'man fopen' for the manuals
>--< printf("file = %d\n", file);
>--<
>--< ret = write(file, "12345\n", 6);
>--<
>--< printf("ret = %d\n", ret);
>--< if (ret < 0) {
>--< printf("%s\n", strerror(errno));
>--< }
>--<
to close the file
try : fclose(FILE *pointer);
in your case it is
fclose(file);
>--< ret = close(file);
>--< printf("ret = %d\n", ret);
>--<}
>--<
>--<_______________________________________________
>--<This is the Linux C Programming List
>--<: http://lists.linux.org.au/listinfo/linuxcprogramming List
>--<
>--<
cheers
biki
--
********************************************************************************
"My programs donot have any bugs, but they sure develope random features!!!"
More information about the linuxCprogramming
mailing list