From: Henrik Carlqvist on 11 Aug 2010 17:43 root <NoEMail(a)home.org> wrote: > The problem, of course, is that I open the file > with mode "r" instead of O_RDONLY. Then penalty > is that the contents of the file are erased. > > This code has worked under every previous version > of gcc. Just to make sure *you* haven't made > this mistake, go to your source directory and do: > > grep ' open' *.c|grep '"r"' > > and see what you get. root.c: -8<------------------------ #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, char **argv) { int fd; off_t strt=0; off_t fsize; char *ptri="root.txt"; fd=open(ptri,"r"); lseek(fd,0L,SEEK_END); fsize=lseek(fd,strt,SEEK_CUR); close(fd); if(fsize==0){ printf("Empty file:%s\n", ptri); } return 0; } -8<------------------------ gcc -o r root.c root.c: In function 'main': root.c:15: warning: passing argument 2 of 'open' makes integer from pointer without a cast /usr/include/fcntl.h:85: note: expected 'int' but argument is of type 'char *' If I would have made such a mistake it seems as if gcc should have told me... clumsy_root.c: -8<------------------------ #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(int argc, char **argv) { int fd; off_t strt=0; off_t fsize; char *ptri="root.txt"; fd=open(ptri,"r"); lseek(fd,0L,SEEK_END); fsize=lseek(fd,strt,SEEK_CUR); close(fd); if(fsize==0){ printf("Empty file:%s\n", ptri); } return 0; } -8<------------------------ gcc -o r clumsy_root.c No error output! gcc -Wall -o r clumsy_root.c root.c: In function 'main': root.c:15: warning: implicit declaration of function 'open' So, to me it seems as if your big mistake was to ignore compiler warnings about calling open in a way which didn't match how open is declared. Or maybe you hadn't include the file where open was declared. Gcc can be really helpful to detect such errors. I like to use the following switches for gcc: -Wall -ansi -pedantic But then, today not all code are supposed to be ansi compatible anymore, sometimes another standard like c90 i preferred and sometimes the pedantic warnings might not be wanted. regards Henrik -- The address in the header is only to prevent spam. My real address is: hc123(at)poolhem.se Examples of addresses which go to spammers: root(a)localhost postmaster(a)localhost |