新建资料失败时出现莫名奇妙的调试信息
新建文件失败时出现莫名奇妙的调试信息
用gcc编译运行:
[root@localhost c]# gcc a.c
[root@localhost c]# ./a.out
you have not input filename,please try again!
: Success
[root@localhost c]#
看上面多了一个:Success字符串,哪位大虾帮我分析下,另外如果在命令行输入2个或2个以上参数,文件创建时正常的
程序如下:
1 #include<stdio.h>
2 #include<fcntl.h>
3 #include<sys/types.h>
4 #include<sys/stat.h>
5 #include<stdlib.h>
6 int main( int argc, char *argv[] )
7 {
8 int i=0;
9 int creat_file( char *filename );
10 if( argc<2 )
11 {
12 perror("you have not input filename,please try again!\n");
13 return 1;
14 }
15
16 for( i=1; i<argc; i++)
17 creat_file( argv[i] );
18 // exit( -1 );
19 return 0;
20 }
21
22 int creat_file( char *filename )
23 {
24 if( ( creat(filename, 0755))==-1)
25 {
26 printf("creat %s file failure", filename);
27 //exit( -1 );
28 return 1;
29 }
30 else
31 printf("creat %s file succuss!\n", filename);
32
33 }
------解决方案--------------------
: Success
以上是perror生成的。
perror( ) 用来将上一个函数发生错误的原因输出到标准设备(stderr)。参数 s 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量errno 的值来决定要输出的字符串。
在库函数中有个errno变量,每个errno值对应着以字符串表示的错误类型。当你调用"某些"函数出错时,该函数已经重新设置了errno的值。perror函数只是将你输入的一些信息和现在的errno所对应的错误一起输出。
用gcc编译运行:
[root@localhost c]# gcc a.c
[root@localhost c]# ./a.out
you have not input filename,please try again!
: Success
[root@localhost c]#
看上面多了一个:Success字符串,哪位大虾帮我分析下,另外如果在命令行输入2个或2个以上参数,文件创建时正常的
程序如下:
1 #include<stdio.h>
2 #include<fcntl.h>
3 #include<sys/types.h>
4 #include<sys/stat.h>
5 #include<stdlib.h>
6 int main( int argc, char *argv[] )
7 {
8 int i=0;
9 int creat_file( char *filename );
10 if( argc<2 )
11 {
12 perror("you have not input filename,please try again!\n");
13 return 1;
14 }
15
16 for( i=1; i<argc; i++)
17 creat_file( argv[i] );
18 // exit( -1 );
19 return 0;
20 }
21
22 int creat_file( char *filename )
23 {
24 if( ( creat(filename, 0755))==-1)
25 {
26 printf("creat %s file failure", filename);
27 //exit( -1 );
28 return 1;
29 }
30 else
31 printf("creat %s file succuss!\n", filename);
32
33 }
------解决方案--------------------
: Success
以上是perror生成的。
perror( ) 用来将上一个函数发生错误的原因输出到标准设备(stderr)。参数 s 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量errno 的值来决定要输出的字符串。
在库函数中有个errno变量,每个errno值对应着以字符串表示的错误类型。当你调用"某些"函数出错时,该函数已经重新设置了errno的值。perror函数只是将你输入的一些信息和现在的errno所对应的错误一起输出。