---------------3.24-----------
stdio.h:C的标准输入输出库;I/O(input output)
file*:结构体
最上面的是文件描述符
第二层是input(键盘文件)
第三层是output
1.stdout:输出流
行缓冲:stdout在终端上进行输出的时候,输出的规则为,每当出现换行符的时候进行一次刷新缓存,然后在进行操作(printf输出的时候,是看到换行符才进行输出的)
2.fopen():
r:只读
w:只写:如果文件不存在,创建一个新的,如果文件存在,清空原先文件内容
a:追加;若不存在创建一个,如果存在,在文件末尾进行追加
r+:可读可写,若文件不存在,打开失败
w+:w + r+
a+:a + r+
注:
不要忘了fclose(fp)
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp;
fp = fopen("test.txt", "w+");
if(NULL == fp){
perror("error\n");
//return 1;
exit(1);
}
fclose(fp);
return 0;
}
**3.fread(void ptr, size_t size, size_t nmemb, FILE stream);
ptr:字符串指针
size:读取每个字符的大小
nmenb:读多少个
stream:文件描述符指针
4.fseek
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fp;
fp = fopen("./text.txt", "w+" );
if(NULL == fp){
perror("open!");
exit(1);
}
char *str = "hello world i love you!";
int number = fwrite(str,sizeof(char),strlen(str),fp);
if(0 ==number){
perror("fwrite!");
fclose(fp);
exit(1);
}
fseek(fp,0,SEEK_SET);
char buffer[1024]={0};
int number2=fread(buffer,sizeof(char),1024,fp);
if(0 == number2){
perror("fread");
fclose(fp);
exit(1);
}
printf("%s\n",buffer);
fclose(fp);
return 0;
}
5.读字符的家族:
(1).getc:从文件里读取一个字符getc(fp),返回值:当读到文件末尾五字符的时候,返回EOF(NULL)
(2).fgetc:函数调用
(3).getchar():将字符从unsigned char 转换成int进行返回
带走缓冲区里多余的换行符
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fp;
int c;
fp = fopen("./text.txt","r");
while((c = getc(fp)) != EOF ){
printf("%c",c);
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fp;
int c;
int i = 0;
for(i = 0;i<5;i++){
c=getchar();
printf("%c ",c);
}
printf("\n");
return 0;
}
6.写字符家族:
(1).fputc:将某一字符写入文件流
int fputc(int c,FILE *stream);
putc:宏(以#开头一半出现在预处理阶段,不检查语法的正确性)
(2).putchar©:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fp;
fp = fopen("./text.txt","w+");
char *str = "hello world! i love you";
int i;
for(i=0;i<strlen(str);i++){
fputc(*(str + i),fp);
}
fclose(fp);
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fp;
int c;
fp = fopen("./text.txt","w+");
char *str = "i love you";
int i;
for(i=0;i<strlen(str);i++){
// fputc(*(str + i),fp);
putchar(*(str +i));
}
fclose(fp);
printf("\n");
return 0;
}
7.printf家族 和 scanf家族
(1).printf
(2).fprintf:
int fprintf(FILE *stream,const char *format,…);
stdout
(3).sprintf():往字符串里写字符串
(4).fscanf():从文件流里写读字符串
int fscanf(FILE *stream, const char *format, …);
stdin
(5).sscanf():从字符串里读取变量
int sscanf(const char *str, const char *format, …);
(1).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fp;
int i,j;
i=10;
j=20;
double l=3.1415926;
fprintf(stdout,"%d %d %.7lf\n",i,j,l);
return 0;
}
(2).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char str[1024]={0};
int i,j,c;
i=10;
j=20;
double l=3.1415926;
sprintf(str,"hello world! %d %d %.7f",i,j,l);
printf("%s",str);
printf("\n");
return 0;
}
(3).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
long i;
int j;
char s[1024]={0};
char input[]="20180505227 21 szj";
sscanf(input,"%ld %d %s",&i,&j,s);
printf("%ld %d %s",i,j,s);
printf("\n");
return 0;
}