//打印默认打开的流的文件描述符
#include<stdio.h>
int main()
{
printf("%d\t",stdin->_fileno);
printf("%d\t",stdout->_fileno);
printf("%d\n",stderr->_fileno);
}
//测试一个进程能打开的文件描述符的最大值
#include<stdio.h>
int main()
{
int n = 3;
FILE *p;
while((p = fopen("fileno.c","r")) != NULL)
{
n++;
}
printf("number:%d\n", n);
return 0;
}
//按字节复制文件
int c;
while((c=fgetc(in_fp))!= EOF)
{
fputc(c,out_fp);
}
//按行复制文件
while(fgets(buf, BUFFERSIZE, in_fp) != NULL)
{
fputs(buf, out_fp);
}
//计算文件中的行数
if(buf[strlen(buf)-1] == '\n') count++;
//按指定大小读写一个流
while((n = fread(buf, 1, 128, in_fp))>0)
{
fwrite(buf, 1, n, out_fp);
}
//按字节复制文件实例
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
FILE *in_fp, *out_fp;
char c;
if(argc < 3)
{
fprintf(stderr,"usage:%s source destination\n", argv[0]);
exit(1);
}
if((in_fp = fopen(argv[1],"r")) == NULL)
{
fprintf(stderr, "can't open %s", argv[1]);
perror(argv[1]);
exit(1);
}
if((out_fp = fopen(argv[2],"w+")) == NULL)
{
fprintf(stderr,"can't open %s", argv[2]);
perror(argv[2]);
exit(1);
}
while((c=fgetc(in_fp))!= EOF)
{
fputc(c,out_fp);
}
if(fclose(in_fp)==-1 || fclose(out_fp)==-1)
{
fprintf(stderr,"can't close");
perror("");
exit(1);
}
return 0;
}
//错误输出
#include <string.h>
#include <errno.h>
printf("fail to fopen %s : %s\n", argv[2], strerror(errno));
fprintf(stderr,"can't open");
perror(argv[2]);//打印默认打开的流的文件描述符
#include<stdio.h>
i