Originally Posted by
tabstop
I think your "generally the environment returns an error" part is perhaps mistaken. I just tried this code:
Code:
#include <stdio.h>
int main(void) {
FILE *this;
this = fopen("test.c", "r");
fprintf(this, "Waahaahaa!\n");
fclose(this);
return 0;
}
where test.c is the file itself, compiled it, and ran it. I didn't get any runtime error, but clearly "Waahaahaa!" was not written in my file either. (I'm running Mac OSX + gcc.)
when i ran your code...sure enough...there was not problem in execution...i though there might be something in stderr...but there was nothing there too...
Code:
[c_d@localhost C scratchpad]$ gcc temp.c
[c_d@localhost C scratchpad]$ touch test.c
[c_d@localhost C scratchpad]$ ./a.out 2>error
[c_d@localhost C scratchpad]$ cat test.c
[c_d@localhost C scratchpad]$ cat error
oh! so theres the flaw...in the C library functions...because...if i use (linux/unix)system calls...then
Code:
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
int fd;
if((fd=open("f1",O_RDONLY))==-1)
{
perror("f1 error");
exit(1);
}
if((write(fd,"arrgh!",6))==-1)
{
perror("error writing to file");
}
close(fd);
}
and the output would be
Code:
[c_d@localhost C scratchpad]$ gcc temp.c
[c_d@localhost C scratchpad]$ touch f1
[c_d@localhost C scratchpad]$ ./a.out
error writing to file: Bad file descriptor
[c_d@localhost C scratchpad]$
see? the environment in this case returned an error...
and if i change the mode from O_RDONLY(read only) to O_WRONLY(write only) or O_RDWR(read-write) i get no errors what so ever
Code:
[c_d@localhost C scratchpad]$ gcc temp.c #compiling after modifying to O_WRONLY
[c_d@localhost C scratchpad]$ ./a.out
[c_d@localhost C scratchpad]$ cat f1
arrgh![c_d@localhost C scratchpad]$
i dont understand this because , library functions would ultimately invoke the system-calls...