愚蠢的问题。 [英] Dumb Question.

查看:73
本文介绍了愚蠢的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>


int main()

{

FILE * fp;

char s [100];


fopen(" file.txt"," w +");


fprintf(fp,HI \ n);


fclose(fp);

fscanf(fp,"%s" ,s);

printf("%s \ n",s);

}


我知道这个将调用未定义的行为。未定义的行为

是否意味着它可能在这种情况下按预期工作?是否可以从已经关闭至少一次的文件中读取


千种不同环境的1000次?

#include <stdio.h>

int main()
{
FILE *fp;
char s[100];

fopen("file.txt","w+");

fprintf(fp,"HI\n");

fclose(fp);
fscanf(fp,"%s",s);
printf("%s\n",s);
}

I know this will invoke undefined behaviour.Does undefined behavior
mean it MIGHT also work as expected in this case?Is it possible to read
from a file which already closed at least once out of 1000 times on
thousand different environment?

推荐答案

va ****** @ rediffmail.com 写道:
va******@rediffmail.com wrote:
#include< stdio.h>

int main()
{
FILE * fp;
char s [100];

fopen(" file.txt", " W +");


检查fopen()的返回值是个好主意。

在使用它之前初始化fp是一个更好的想法...

fprintf(fp,HI \ n ;);

fclose(fp);
fscanf(fp,"%s",s);
printf("%s \ n",s) ;
}
我知道这将调用未定义的行为。未定义的行为
意味着它可能在这种情况下按预期工作吗?


从未定义的行为中得到任何东西真的是一个坏主意。



之前这张海报说他计算机实际上已经问过他如果他想重新格式化C:在一个特别糟糕的UB例子之后。

是否可以阅读
来自一个文件已经在1000个不同的环境中至少关闭了1000次?
#include <stdio.h>

int main()
{
FILE *fp;
char s[100];

fopen("file.txt","w+");
it''s a good idea to check the return value of fopen(). It is an even
better idea to
initialise fp before you use it...
fprintf(fp,"HI\n");

fclose(fp);
fscanf(fp,"%s",s);
printf("%s\n",s);
}

I know this will invoke undefined behaviour.Does undefined behavior
mean it MIGHT also work as expected in this case?
it really is a bad idea to expect anything from Undefined Behaviour.
Long
ago on this ng a poster said he had actually been asked by the computer

if he wanted to reformat C: after a particularly bad example of UB.
Is it possible to read
from a file which already closed at least once out of 1000 times on
thousand different environment?




我怀疑大多数平台都不会让你写信给关闭文件。为什么

你会想要这么做吗?你可能会活着用手指插入电源插座,

但是为什么这样做?

-

Nick Keighley



I suspect most platforms will not ley you write to a closed file. Why
would you
want to do this? You might survive sticking your fingers in a live
electrical socket,
but why do that?
--
Nick Keighley




vashw ... @ rediffmail.com schrieb:

vashw...@rediffmail.com schrieb:
#include< stdio.h>

int main()
{
FILE * fp;
char s [100];
< br> fopen(" file.txt"," w +");

fprintf(fp," HI \ n");

fclose (fp);
fscanf(fp,"%s",s);
printf("%s \ n",s);
}

我知道这将调用未定义的行为。未定义的行为
意味着它可能在这种情况下按预期工作吗?
#include <stdio.h>

int main()
{
FILE *fp;
char s[100];

fopen("file.txt","w+");

fprintf(fp,"HI\n");

fclose(fp);
fscanf(fp,"%s",s);
printf("%s\n",s);
}

I know this will invoke undefined behaviour.Does undefined behavior
mean it MIGHT also work as expected in this case?




不,这意味着,它在大多数情况下,实际上会按预期工作。当

使用未初始化的指针时,我预计会出现分段错误。但是,变量fp可能与stdin具有相同的值,因此

一旦在几百万年内就会从stdin读取一些内容。然而,

将是完全出乎意料的行为。



No, it means, it will actually work as expected in most cases. When
using an uninitialized pointer, I expect a segmentation fault. However,
the variable fp could by chance have the same value as stdin, so that
once in some million years it would read something from stdin. This,
however, would be totally unexpected behaviour.


va ****** @ rediffmail.com 写道:
va******@rediffmail.com wrote:
#include< stdio.h>

int main()
{
FILE * fp;
char s [100];

fopen(" file.txt"," w + ");

fprintf(fp," HI \ n");

fclose(fp);
fscanf(fp,"%s" ;,s);
printf("%s \ n",s);
}
我知道这将调用未定义的行为。未定义的行为
意味着它可能在这种情况下按预期工作吗?
#include <stdio.h>

int main()
{
FILE *fp;
char s[100];

fopen("file.txt","w+");

fprintf(fp,"HI\n");

fclose(fp);
fscanf(fp,"%s",s);
printf("%s\n",s);
}

I know this will invoke undefined behaviour.Does undefined behavior
mean it MIGHT also work as expected in this case?




当然,如果你想冒风险,请随意。如果你想冒险在你的测试机器上工作正常,但是你的用户'/ b $ b不同的盒子上的数据会被破坏,你的用户也可以这样做,去吧对了

领先。但绝对不要在_my_数据上尝试这样的技巧。


Richard



Sure, if you want to risk your neck, feel free. If you want to risk it
working fine on your testing machine, but corrupting data on your users''
differently specced boxes, and your users are OK with that, go right
ahead. Make absolutely sure not to try such tricks on _my_ data, though.

Richard


这篇关于愚蠢的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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