Click here to Skip to main content
15,886,080 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
When I study c programming on linux,I see the linux API.
The file function: int open( const char * pathname,int flags, mode_t mode);
and it said,if success return file descriptor,else -1.
if it means we can't decide when it return -1 and when success?
Suppose that,if it means we can't ensure the validity of a program.But how can we realize a system like oracle DBA.
And most demos in books often use many "if statement' to judge the result and then do the next step.
For example:
C++
int f;
f=open("outfile",O_RDWR|O_CREAT,0664);
if(f!=-1){
 if(write(f,"12345",i)!=i)
       perror("write error");
}
else
 perror("open failed");

Since we don't know how it return -1,why we judge it?Even if we judge it,we can get nothing.Why not ingore it,and then at the end of the program,we just give a output and tell it successes.If we have not get the output,means we failed.And according to my solution,we leave many "if statement" out,our program's speed will improve.
my code:
C++
int f;
open("outfile",O_RDWR|O_CREAT,0664);
write(f,"12345",i);
printf("success\n");

why not we did this?Can you help me,and I will appreciate it.
Posted
v2
Comments
[no name] 6-Mar-14 4:31am    
A truly existential question. Maybe you should try philosophy.

"it means we can't decide when it return -1 and when success?"
No, you can't - that's the whole point.

Think of it this way: you want to open a file so you can write some stuff into it, so you call the method to open the file. If the file is already open because some other program is writing to the same file, you can't open it - but you can't predict that. If it can't open it for any reason, it returns an error value.

Perhaps an analogy will help: you are driving down the road and understandably don't want to stop. Ahead is a set of traffic lights: you have no control over the lights - they are on a timer - but if they "go red" you MUST stop. If you don't there is likely to be a nasty accident.
The file open function checks the traffic lights: if they are green, it lets you continue. If they are red it returns a value specifically to let you know so you can respond (and either try again in a moment, or report a problem to your user and do something else)
 
Share this answer
 
Comments
Member 10629059 6-Mar-14 3:48am    
Thank you and your analogy is very good.I understand it.And,if we can know the reason through the error value.If it means an error code represent an reason.And if there is some other reasons leading error has no error code corresponding?
OriginalGriff 6-Mar-14 4:16am    
Simple: the return value tells you there *is* an error - you can then find out exactly *what* the error is.
If you include errno.h in your file, you can find out what the error number is by looking at the global errno variable which the fopen function will set. You can convert this to a string message using the strerror function.
https://www.mkssoftware.com/docs/man3/strerror.3.asp
Member 10629059 6-Mar-14 4:27am    
Thank you again.I got it.
OriginalGriff 6-Mar-14 4:48am    
You're welcome!
That is an odd question. If you call a friend in the opposite end of the world and ask him whether it rains in his place, will you change his response to your preference, too? If your plane crashes, aren't you affected because you simply don't accept that the engine just caught fire?

More importantly: why do you even call a function - any function - if you don't care whether it succeeds or not? Why do you even write a program if you don't care whether it does what it's supposed to do?
 
Share this answer
 
Comments
Member 10629059 6-Mar-14 4:32am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900