//转自陈颢文(老师)的blog
//buffer.c
#include <stdio.h>
//练习:自己打印一下,全缓存,行缓存和不缓存的大小
int main(int argc, const char *argv[])
{
int data = 0;
//注意:当我们只有进行实际的I/O操作的时候,才会刷新我们的缓存。
scanf("%d",&data);
FILE *fp = stdin;
printf("stdin_buff : %d\n",fp->_IO_buf_end - fp->_IO_buf_base);
fp = stdout;
printf("stdout_buff : %d\n",fp->_IO_buf_end - fp->_IO_buf_base);
fp = stderr;
printf("stderr_buff : %d\n",fp->_IO_buf_end - fp->_IO_buf_base);
return 0;
}
=========================================================
//copy_char.c
#include <stdio.h>
//a.out src_file dest_file
int main(int argc, const char *argv[])
{
FILE *fp_src = NULL;
FILE *fp_dest = NULL;
int ch = 0;
if(argc < 3)
{
fprintf(stderr,"Usage : %s argv[0] argv[1]",argv[0]);
return -1;
}
//源文件以只读的方式打开
fp_src = fopen(argv[1],"r");
if(NULL == fp_src)
{
perror("Fail to fopen src");
return -1;
}
//目标文件以只写的方式打开
fp_dest = fopen(argv[2],"w");
if(NULL == fp_dest)
{
perror("Fail to fopen dest");
return -1;
}
while(1)
{
ch = fgetc(fp_src);
if(ch == EOF)
break;
fputc(ch,fp_dest);
}
return 0;
}
=========================================================
fgetc_fputc.c
#include <stdio.h>
//stdout
//stdin
//stderr
int main(int argc, const char *argv[])
{
int ch = 0;
while(1)
{
ch = fgetc(stdin);
if(EOF == ch)
{
printf("ch = %c : %d\n",ch,ch);
break;
}
fputc(ch,stdout);
}
return 0;
}
============================================
fgetc_fputc1.c
#include <stdio.h>
//stdout
//stdin
//stderr
int main(int argc, const char *argv[])
{
int ch = 0;
FILE *fp = NULL;
if(argc < 2)
{
fprintf(stderr,"Usage : %s filename\n",argv[0]);
return -1;
}
fp = fopen(argv[1],"r");
if(NULL == fp)
{
perror("Fail to fopen");
return -1;
}
while(1)
{
ch = fgetc(fp);
if(EOF == ch)
{
break;
}
fputc(ch,stdout);
}
return 0;
}
=========================================
fopen.c
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE *fp;
fp = fopen("test.c","r");
if(NULL == fp)
{
/*printf("Fail to fopen\n");*/
// printf("errno : %d\n",errno);
//fprintf(stderr,"Fail to fopen : %s\n",strerror(errno));
perror("Fail to fopen");
}
return 0;
}
==================================================
fopen_home.c
#include <stdio.h>
//./a.out newfile
//argv[0] ./a.out
//argv[1] "newfile"
//
//argc = 2;
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
int i = 0;
if(argc != 2)
{
fprintf(stderr,"Usage : %s filename",argv[0]);
return -1;
}
fp = fopen(argv[1],"w");
//fp = fopen("newfile","w");
if(NULL == fp)
{
perror("Fail to fopen");
return -1;
}
for(i = 1;i <= 10;i++)
{
fprintf(fp,"%d ",i);
}
fclose(fp);
fclose(fp);
//while(1);
return 0;
}
==============================================
fprintf_sprintf.c
#include <stdio.h>
#include <string.h>
#define MAX 100
int main(int argc, const char *argv[])
{
char buf[MAX] = {0};
printf("Code is start.......\n");
#if 0
fprintf(stdout,"STDOUT : I am a %s boy!\n","handsome");
fprintf(stderr,"STDERR : I love you ,girl?\n");
sprintf(buf,"really ? I am so happy!");
puts(buf); //printf("%s\n",buf);
#endif
fclose(stdout);
fclose(stderr);
fprintf(stdout,"hello world\n");
fprintf(stderr,"Fail to create : no such file or directory!\n");
sprintf(buf,"welcome to wuhan");
puts(buf);
#if 0/*{{{*/
memset(buf,0,sizeof(buf));
sprintf(buf,"Hi,%s, I love you too.","chw");
puts(buf);
#endif/*}}}*/
return 0;
}
=====================================
fprintf_sprintf_home.c
#include <stdio.h>
int main(int argc, const char *argv[])
{
int a = 123;
char buf[100] = {0};
int i = 0;
printf("%d\n",a);
sprintf(buf,"%d",a);
fprintf(stdout,"%s\n",buf);
for(i = 0;buf[i] != '\0';i++)
{
printf("%c ",buf[i]);
}
putchar('\n');
return 0;
}