这有什么不对 [英] what's wrong with this

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

问题描述

嗨朋友们,


你能告诉我这段代码有什么问题。


#include< stdio.h>

#include< math.h>


main(){


FILE * fp;


fp = fopen(" file.txt"," r");

if(fp == NULL){

printf(" \ n无法打开file.txt文件。\ n");

退出(1);

}

否则{

printf(" \\\
file.txt文件已打开。\ n);


}

}


file.txt没有任何价值,但令人惊讶的是,当我运行上面的代码时,我收到了

以下消息


file.txt文件已打开。


你能告诉我为什么我会这样做吗?


提前致谢。

ss ..

-

留言使用 http://www.talkabo utprogramming.com/group/comp.lang.c/

更多信息,请访问 http://www.talkaboutprogramming.com/faq.html

解决方案



" rsk" < kr ****** @ yahoo.co.inschrieb im Newsbeitrag

新闻:ee ********************** ********@localhost.ta lkaboutprogramming.com ...


嗨朋友们,


可以你告诉我这段代码有什么问题。


#include< stdio.h>

#include< math.h>


main(){



int main(void)


> ;

FILE * fp;


fp = fopen(" file.txt"," r");

if(fp == NULL){

printf(" \ n无法打开file.txt文件。\ n");

exit(1); < br $>
}

else {

printf(" \\\
file.txt File is Opened.\\\
);


}



返回0;


}


file.txt没有任何价值,但令人惊讶的是我获得了


?你的意思是该文件是空的?或者它不存在?


以下消息当我运行上面的代码时


file.txt文件已打开。


你能告诉我为什么我得到这个?



因为它已成功打开。这意味着该文件存在于

之前(或您的实现有错误)

再见,Jojo




" Joachim Schmitz" < no ********* @ schmitz-digital.deschrieb im Newsbeitrag

news:fd ********** @ online.de ...


>

" rsk" < kr ****** @ yahoo.co.inschrieb im Newsbeitrag

新闻:ee ********************** ********@localhost.ta lkaboutprogramming.com ...


>嗨朋友们,

你能说一遍吗告诉我这段代码有什么不对。

#include< stdio.h>
#include< math.h>

main(){



int main(void)


>>
FILE * fp;

fp = fopen(" file.txt"," r");
if(fp == NULL){
printf(" \ n无法打开file.txt file.\\\
");
exit(1);
}
else {
printf(" \\\
file.txt File is Opened.\\\
" );

}



返回0;


>}

file.txt没有任何价值,但令人惊讶的是我得到



?你的意思是该文件是空的?或者它不存在?


>当我运行上面的代码时,以下消息

file.txt文件已打开。

你能告诉我为什么我得到这个?



因为它已成功打开。这意味着该文件存在

之前(或您的实现有错误)



7.19.5.3-4

如果文件不存在或无法读取,打开具有读取模式的文件(''r''作为模式

参数中的第一个字符)将失败。


rsk写道:


嗨朋友们,


你能告诉我什么是错的这段代码。


#include< stdio.h>

#include< math.h>



你在math.h中没有使用任何东西


main(){



更好:

int main(void){/ * ... * /}


FILE * fp;


fp = fopen(" file.txt"," r");

if(fp == NULL ){

printf(" \ n无法打开file.txt文件。\ n");

exit(1);



一个不是便携式状态值。使用0,EXIT_SUCCESS或

EXIT_FAILURE中的一个。后两者在stdlib.h中定义。前两个是或多或少可互换的。


}

else {

printf(" \\\
file.txt文件已打开。\ n");


}



并从main返回一个值。


}


file.txt没有任何价值,但令人惊讶的是我得到了当我运行上面的代码时,

以下消息


file.txt文件已打开。


你能来吗请告诉我为什么我得到这个?



您确定该文件不存在。或者它是否存在,但是是空的。如果

以前的情况,那么你的实施就会被破坏,但很可能是

后一种情况。


Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

main () {

FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}
}

The file.txt is not having any value but surprisingly iam getting the
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?

Thanks in advance.
ss..
--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

解决方案


"rsk" <kr******@yahoo.co.inschrieb im Newsbeitrag
news:ee******************************@localhost.ta lkaboutprogramming.com...

Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

main () {

int main(void)

>
FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}

return 0;

}

The file.txt is not having any value but surprisingly iam getting the

? You mean that file is empty? Or it doesn''t exist?

following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?

Because it got opened successfully. Which means that the file existed before
(or your implementation has a bug)
Bye, Jojo



"Joachim Schmitz" <no*********@schmitz-digital.deschrieb im Newsbeitrag
news:fd**********@online.de...

>
"rsk" <kr******@yahoo.co.inschrieb im Newsbeitrag
news:ee******************************@localhost.ta lkaboutprogramming.com...

>Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

main () {

int main(void)

>>
FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}

return 0;

>}

The file.txt is not having any value but surprisingly iam getting the

? You mean that file is empty? Or it doesn''t exist?

>following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?

Because it got opened successfully. Which means that the file existed
before (or your implementation has a bug)

7.19.5.3-4
Opening a file with read mode (''r'' as the first character in the mode
argument) fails if the file does not exist or cannot be read.


rsk wrote:

Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

You don''t use anything in math.h

main () {

Better:
int main(void) { /* ... */ }

FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);

One is not a portable status value. Use one of 0, EXIT_SUCCESS or
EXIT_FAILURE. The latter two are defined in stdlib.h. The first two are
more or less interchangeable.

}
else {
printf("\n file.txt File is Opened.\n");

}

And return a value from main.

}

The file.txt is not having any value but surprisingly iam getting the
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?

Are you sure the file did not exist. Or does it exist, but is empty. If the
former case, then your implementation is broken, but it''s much probably the
latter case.


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

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