LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1171|回复: 8

请教:if(stream=fopen("test","r")==(FILE*)0)

[复制链接]
发表于 2006-6-30 12:09:49 | 显示全部楼层 |阅读模式
请教:
#include "stdio.h"

main()
{
        char key;
        FILE *stream;            /*流的指针*/
       
        printf("please input a letter \n");
       
        key=getchar();           /*读入一个字符*/
       
        if(stream=fopen("test","r")==(FILE*)0)  /*将流与一个文件联系起来*/
          {
                  //判断是否成功
                  fprintf(stderr,"Error opening file.\n");
                  exit(1);
          }
}

请教:
if(stream=fopen("test","r")==(FILE*)0)

这句话是什么意思?

还有 (FILE*)0) 是什么意思?

谢谢!
发表于 2006-6-30 12:34:43 | 显示全部楼层
就是如果文件打开失败了

(FILE*)0) 就是0,就是NULL指针,强制类型转换成FILE*
回复 支持 反对

使用道具 举报

发表于 2006-6-30 13:59:48 | 显示全部楼层
stream=fopen("test","r");
if(stream==NULL) {
    .....
}
回复 支持 反对

使用道具 举报

发表于 2006-6-30 14:36:21 | 显示全部楼层
不懂为什么要写成那样?
if((stream = fopen("test","r")) == NULL) 这样不是很好吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-6-30 14:37:48 | 显示全部楼层
谢谢 楼上各位大哥!

#include "stdio.h"

main()
{
char key;
FILE *stream; /*流的指针*/

printf("please input a letter \n");

key=getchar(); /*读入一个字符*/

//if(stream=fopen("test","r")==(FILE*)0) /*将流与一个文件联系起来*/
if ((stream=fopen("test","r"))==NULL)
{
//判断是否成功
fprintf(stderr,"Error opening file.\n");
exit(1);
}

/*将缓冲区设定为行缓冲,并判断是否成功*/

if (setlinebuf(stream))!=0)
{
printf(stderr,"Error setlinebuffer/\n");
exit(1);
}

/*将对应缓冲区清空,并判断操作是否成功*/

if (fpurge(stream))==EOF)
{
fprintf(stderr,"Error flush stream.\n");
exit(1);
}

/*对流读写操作*/

fprintf(stream,"The letter that you input is %c .\n",Key);

/*在程序结束之前关闭流,并判断操作是否成功*/

if(fclose(stream))==EOF)
{
fprintf(stderr,"Error closing file \n");
exit(1);
}


}

编译时出现的信息是:
[root@localhost linuxss]# gcc -ggdb3 -o text3 text3.c
text3.c: In function `main':
text3.c:22: void value not ignored as it ought to be
text3.c:22: parse error before '!=' token
text3.c: At top level:
text3.c:30: parse error before "if"
text3.c:33: parse error before numeric constant
text3.c:33: warning: data definition has no type or storage class
text3.c:38: parse error before string constant
text3.c:38: warning: conflicting types for built-in function `fprintf'
text3.c:38: warning: data definition has no type or storage class
text3.c:45: parse error before numeric constant
text3.c:45: warning: data definition has no type or storage class
text3.c:51:1: warning: no newline at end of file
[root@localhost linuxss]#


各位大哥,我需要修改哪些东西呢?

谢谢!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-6-30 15:11:24 | 显示全部楼层
#include "stdio.h"

main()
{
char key;
FILE *stream; /*流的指针*/

printf("please input a letter \n");

key=getchar(); /*读入一个字符*/


if ((stream=fopen("test","r"))==NULL)
{

fprintf(stderr,"Error opening file.\n");
exit(1);
}

/*将缓冲区设定为行缓冲,并判断是否成功*/

if ((setlinebuf(stream))!=0)
{
printf(stderr,"Error setlinebuffer/\n");
exit(1);
}

/*将对应缓冲区清空,并判断操作是否成功*/

if ((fpurge(stream))==EOF)
{
fprintf(stderr,"Error flush stream.\n");
exit(1);
}

/*对流读写操作*/

fprintf(stream,"The letter that you input is %c .\n",Key);

/*在程序结束之前关闭流,并判断操作是否成功*/

if((fclose(stream))==EOF)
{
fprintf(stderr,"Error closing file \n");
exit(1);
}


}



我修改了括号,还是有错误哦:
[root@localhost linuxss]# gcc -ggdb3 -o text3 text3.c
text3.c: In function `main':
text3.c:22: void value not ignored as it ought to be
text3.c:24: warning: passing arg 1 of `printf' from incompatible pointer type
text3.c:38: `Key' undeclared (first use in this function)
text3.c:38: (Each undeclared identifier is reported only once
text3.c:38: for each function it appears in.)
text3.c:51:1: warning: no newline at end of file


我还要修改哪些地方啊?
谢谢!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-6-30 15:11:27 | 显示全部楼层
#include "stdio.h"

main()
{
char key;
FILE *stream; /*流的指针*/

printf("please input a letter \n");

key=getchar(); /*读入一个字符*/


if ((stream=fopen("test","r"))==NULL)
{

fprintf(stderr,"Error opening file.\n");
exit(1);
}

/*将缓冲区设定为行缓冲,并判断是否成功*/

if ((setlinebuf(stream))!=0)
{
printf(stderr,"Error setlinebuffer/\n");
exit(1);
}

/*将对应缓冲区清空,并判断操作是否成功*/

if ((fpurge(stream))==EOF)
{
fprintf(stderr,"Error flush stream.\n");
exit(1);
}

/*对流读写操作*/

fprintf(stream,"The letter that you input is %c .\n",Key);

/*在程序结束之前关闭流,并判断操作是否成功*/

if((fclose(stream))==EOF)
{
fprintf(stderr,"Error closing file \n");
exit(1);
}


}



我修改了括号,还是有错误哦:
[root@localhost linuxss]# gcc -ggdb3 -o text3 text3.c
text3.c: In function `main':
text3.c:22: void value not ignored as it ought to be
text3.c:24: warning: passing arg 1 of `printf' from incompatible pointer type
text3.c:38: `Key' undeclared (first use in this function)
text3.c:38: (Each undeclared identifier is reported only once
text3.c:38: for each function it appears in.)
text3.c:51:1: warning: no newline at end of file


我还要修改哪些地方啊?
谢谢!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-6-30 16:06:30 | 显示全部楼层

re

include "stdio.h"
#include <stdlib.h>//for exit()
#include <stdio_ext.h>//for void __fpurge(FILE* stream)


main()
{
        char key;
        FILE *stream;            /*流的指针*/
       
        printf("please input a letter \n");
       
        key=getchar();           /*读入一个字符*/
       
       
        if ((stream=fopen("test","r"))==NULL)
          {
                 
                  fprintf(stderr,"Error opening file.\n");
                  exit(1);
          }
          
        /*将缓冲区设定为行缓冲,并判断是否成功*/
       
       
         setlinebuf(stream) ;
         _fpurge(stream);
          
          
        /*将对应缓冲区清空,并判断操作是否成功*/  
       
        if ((fpurge(stream))==EOF)
                {
                        fprintf(stderr,"Error flush stream.\n");
                        exit(1);
                }
               
  /*对流读写操作*/
  
  fprintf(stream,"The letter that you input is %c .\n",key);
  
  /*在程序结束之前关闭流,并判断操作是否成功*/               
  
  if((fclose(stream))==EOF)
          {
                  fprintf(stderr,"Error closing file \n");
            exit(1);
          }
         

}                                                      


编译时提示信息:

[root@localhost linuxss]# gcc -ggdb3 -o text3 text3.c
text3.c:53:1: warning: no newline at end of file
/tmp/cciGLnUk.o(.text+0x7b): In function `main':
/mnt/hgfs/linuxss/text3.c:27: undefined reference to `_fpurge'
/tmp/cciGLnUk.o(.text+0x89):/mnt/hgfs/linuxss/text3.c:32: undefined reference to `fpurge'
collect2: ld returned 1 exit status


大哥,我还要改那里啊?
谢谢!
回复 支持 反对

使用道具 举报

发表于 2006-6-30 20:36:33 | 显示全部楼层
[color="Red"]__fpurge
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表