我如何处理这个c编程问题以及我的观点是否正确? [英] How do I deal with this c programming question and if my viewpoint is right?

查看:51
本文介绍了我如何处理这个c编程问题以及我的观点是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在linux上学习c编程时,我看到了linux API。

文件函数:int open(const char * pathname,int flags,mode_t mode);

,它说,如果成功返回文件描述符,否则为-1。

如果这意味着我们无法决定何时返回-1并且成功时是什么?

假设如果这意味着我们无法确保程序的有效性。但是我们怎样才能实现像oracle DBA这样的系统。

书中的大多数演示经常使用许多if statement来判断然后执行下一步。

例如:

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:

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");



因为我们不知道它如何返回-1,为什么我们判断它?即使我们判断它,我们什么也得不到。为什么不进入它,然后在程序结束时,我们只是给出一个输出并告诉它成功。如果我们没有得到输出,意味着我们失败了。根据我的解决方案,我们留下许多if statement,我们的程序速度会提高。

我的代码:


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:

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.

推荐答案

这意味着我们可以' t决定它何时返回-1并且成功时?

不,你不能 - 这就是重点。



想象一下:你想打开一个文件,这样你就可以写一些东西,所以你调用方法来打开文件。如果文件已经打开,因为某些其他程序正在写入同一文件,则无法打开它 - 但您无法预测。如果因任何原因无法打开它,它会返回一个错误值。



也许一个类比会有所帮助:你正在开车,可以理解的是没有想停下来前方是一组红绿灯:你无法控制灯光 - 它们在计时器上 - 但如果它们变红你必须停下来。如果你不这样做可能会有一个令人讨厌的事故。

文件打开功能检查交通信号灯:如果它们是绿色,它可以让你继续。如果它们是红色的,它会返回一个值,专门告诉您,以便您可以回复(并在一会儿再试一次,或向用户报告问题并执行其他操作)
"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)


这是一个奇怪的问题。如果你打电话给世界另一端的朋友并问他是否在他的位置下雨,你会改变他对你偏好的回应吗?如果你的飞机坠毁,你不是因为你根本不接受发动机起火了吗?



更重要的是:为什么你甚至叫一个功能 - 任何功能 - 如果你不关心它是否成功?如果你不关心它是否能完成它应该做的事情,你为什么还要写一个程序?
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?


这篇关于我如何处理这个c编程问题以及我的观点是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆