ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I went ahead and copied your code, and tried it myself. The results were quite surprising; i got the same results as you, though i could see nothing wrong with the code. I noticed that the file handle 'out' was being set to zero, which was causing input to be taken from stdin! So i tried changing the open statement:
Code:
if(out=open(path4,O_RDONLY)==-1)
which i changed to two lines:
Code:
out=open(path4,O_RDONLY);
if(out==-1)
this gave me a valid file handle, and the logic worked fine!
actually the problem is not with opening the file but with reading from file.The file will be opend and the value of out will be 0 it goes into else part but it doeant show the contents of the file.Is my read command right or not?
actually the problem is not with opening the file but with reading from file.The file will be opend and the value of out will be 0 it goes into else part but it doeant show the contents of the file.Is my read command right or not?
Not quite true, since what is returned from the open function is the file descriptor. This is then passed into the read function. However if (as others have pointed out) this is not being set up correctly then the read will fail because it is trying to read from an invalid file descriptor.
I suggest that you change the code of the open as suggested and you should then have a valid fd and will then be able to read the file.
Breaking the if statement into two lines, or enclosing the file handle assignment within quotes in one line ends up achieving the same end. The problem with the original if clause was not a gcc bug as i first thought, but it's just how most C compilers evaluate any expression: from right to left. So the right-most part of the original if clause
Code:
open(path4,O_RDONLY))==-1
evaluates to false or zero, since any valid file handle returned by open() will be a positive integer. The result of this evaluation, which is zero, is then assigned to the variable 'out' in the left part. You don't want the file handle to be zero; that's the file handle for stdin (the keyboard)!
Enclosing the left-hand part in parenthesis forces that part to be evaluated first, which assigns a valid file handle to 'out'.
what i was thinking but lack of a stream anywhere confused me. I see now there using the open command rather then the fopen command.
open is a low level c command for file manipulation it returns a file descriptor. as far as me im not famillar with using it really i always use fopen command cause its eazier to work with.
what i was thinking but lack of a stream anywhere confused me. I see now there using the open command rather then the fopen command.
open is a low level c command for file manipulation it returns a file descriptor. as far as me im not famillar with using it really i always use fopen command cause its eazier to work with.
fopen is std I/O routine which internally makes use of open system call when std I/O buffers get filled.
Rather a write system call associated with open call flushes the buffer each time a write is made.
Hence for frequent writes with less no of characters - fopen and fwrite would suit to.
For less no of writes and bulk characters open and write would fit in.
But still usage depends upon the write frequency, application criticality, integrity needed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.