015_stdc_C语言

回顾:

函数指针

void * 无类型的指针
有类型的指针都可以给 无类型的指针进行赋值

   无类型的指针在使用的时候,要将其转换为具体的某个类型才能使用

内存管理函数
   malloc 返回就是一个无类型的指针
   free  释放动态申请的内存

   calloc  
   realloc 


时间函数
    time();
    ctime

    struct tm *


    localtime()
    gmtime()

  //为需要管理系统,添加录入时间的成员

---------------------------

标准函数库
输入输出函数
printf
scanf

     文件的输入输出

          fopen打开文件
FILE *fopen(const char *path, const char *mode);


1.头文件
#include <stdio.h>

2.参数
const char *path: 要打开文件的路径(文件名)

第二个参数

const char *mode:  

r : 以只读方式打开文件.文件流指针在文件开始位置
r+: 以读/写方式打开文件.文件流指针在文件开始位置

w : 以只写方式打开文件.文件流指针在文件开始位置
文件如果不存在则创建文件,如果文件存在,清空原来文件的内容

w+ : 以读/写方式打开文件.文件流指针在文件开始位置
文件如果不存在则创建文件,如果文件存在,清空原来文件的内容

a : 以追加的方式打开文件.文件流指针在文件末尾位置
文件如果不存在则创建文件,如果文件存在,不清空原来文件的内容

a+ : 以追加的方式打开文件,可读.写入文件流指针在文件末尾位置,如果是读文件流指针在最开始的位置
文件如果不存在则创建文件,如果文件存在,不清空原来文件的内容

3.返回值
       成功返回 指向文件的结构体指针,失败返回NULL

#include <stdio.h> 
int main() {
    FILE *fp=fopen("day133.txt","r");
    if(NULL == fp)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
    fclose(fp); //关闭文件
    fp = NULL; //防止野指针
    getchar();
    return 0; 
}
#include <stdio.h> 
int main() {
    FILE *fp=fopen("day133.txt","w");
    if(NULL == fp)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
    fclose(fp); //关闭文件
     fp = NULL; //防止野指针
    getchar();
    return 0; 
}
文件打开成功

  fwrite写文件

头文件  #include <stdio.h>
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

参数:
   第一个参数   写入到文件内容的内存起始地址 
   第二个参数   写入内容元素 大小
   第三个参数   写入元素的个数
   第四个参数   写入文件的结构体指针(写到哪个文件)

返回值:
    返回实际写入到文件的元素个数 通常返回 第三个参数
#include <stdio.h> 
int main() {
    int b= 97;
    FILE *fp=fopen("day133.txt","w");
    if(NULL == fp)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
    fwrite(&b,sizeof(int),1,fp);//写入一个字符 a 到文件
    fclose(fp); //关闭文件
    fp = NULL; //防止野指针
    getchar();
    return 0; 
}

#include <stdio.h> 
#include <string.h>
typedef struct{
    char name[20];
    int score ;
    int age;
} Student;
int main() {
    int b= 97;
    Student s;
    strcpy(s.name,"如花");
    s.score = 80;
    s.age= 12;
    FILE *fp=fopen("day133.txt","w");
    if(NULL == fp)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
    int i =fwrite(&s,sizeof(s),1,fp);//写入一个学员信息
    printf("i = %d
",i);
    fclose(fp); //关闭文件
    fp = NULL; //防止野指针
    getchar();
    return 0; 
}

fread读文件

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
参数: 第一个参数 将文件内容的内存起始地址 第二个参数 读内容元素 大小 第三个参数 读元素的个数 第四个参数 读文件的结构体指针(从哪个文件获取数据) 返回值: 返回实际从文件读入到内存的元素个数 通常返回 第三个参数
#include <stdio.h> 
#include <string.h>
typedef struct{
    char name[20];
    int score ;
    int age;
} Student;
int main() {
    int b= 97;
    Student s;
    strcpy(s.name,"如花");
    s.score = 80;
    s.age= 12;
    FILE *fp=fopen("day133.txt","r+");
    if(NULL == fp)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
#if 0
    int i =fwrite(&s,sizeof(s),1,fp);//写入一个学员信息
    printf("i = %d
",i);
#endif
#if 1
    fread(&s,sizeof(s),1,fp);
    printf("name = %s
",s.name);
    printf("score = %d
",s.score);
    printf("age = %d
",s.age);
#endif

    fclose(fp); //关闭文件
    fp = NULL; //防止野指针
    getchar();
    return 0; 
}
文件打开成功
name = 如花
score = 80
age = 12

跟文件流指针相关的函数
ftell()
告诉用户 文件流指针当前位置
传入 文件结构体指针
返回 相对于文件起始位置的偏移(字节)

rewind()
复位文件流指针,文件流指针指向最开始的位置
传入 文件结构体
没有返回值

#include <stdio.h> 
#include <string.h>
typedef struct{
    char name[20];
    int score ;
    int age;
} Student;
int main() {
    int b= 97;
    Student s;
    strcpy(s.name,"如花");
    s.score = 80;
    s.age= 12;
    FILE *fp=fopen("day133.txt","r+");
    if(NULL == fp)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
#if 0
    int i =fwrite(&s,sizeof(s),1,fp);//写入一个学员信息
    printf("i = %d
",i);
#endif
#if 1
    //写在最开始的位置
    printf("读前文件流指针 = %ld
",ftell(fp));
    fread(&s,sizeof(s),1,fp);
    printf("name = %s
",s.name);
    printf("score = %d
",s.score);
    printf("age = %d
",s.age);
    printf("读后文件流指针 = %ld
",ftell(fp));
    rewind(fp);
    printf("rewind后文件流指针 = %ld
",ftell(fp));
#endif

    fclose(fp); //关闭文件
    fp = NULL; //防止野指针
    getchar();
    return 0; 
}
文件打开成功
读前文件流指针 = 0
name = 如花
score = 80
age = 12
读后文件流指针 = 29
rewind后文件流指针 = 0

    fseek()

    可以设置 文件流指针,相对于指定位置的偏移

    int fseek(FILE *stream, long offset, int whence);
    第一个参数: 传入设置文件的结构体指针
    第二个参数: 偏移的距离(字节)
    第三个参数: 相对于哪个位置的偏移

    第三个参数  有下面的三个宏(int)
    SEEK_SET:  文件开始
    SEEK_CUR:  当前位置
    SEEK_END:  文件末尾
#include <stdio.h> 
#include <string.h>
typedef struct{
    char name[20];
    int score ;
    int age;
} Student;
int main() {
    int b= 97;
    Student s;
    strcpy(s.name,"如花");
    s.score = 80;
    s.age= 12;
    FILE *fp=fopen("day133.txt","r+");
    if(NULL == fp)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
#if 0
    int i =fwrite(&s,sizeof(s),1,fp);//写入一个学员信息
    printf("i = %d
",i);
#endif
#if 1
    //写在最开始的位置
    printf("读前文件流指针 = %ld
",ftell(fp));
    fread(&s,sizeof(s),1,fp);//将读到的内容放到s结构体里面
    printf("name = %s
",s.name);
    printf("score = %d
",s.score);
    printf("age = %d
",s.age);
    printf("读后文件流指针 = %ld
",ftell(fp));
    fseek(fp,-8,SEEK_END);//设置流文件指针回退8个字节
    printf("fseek后文件流指针 = %ld
",ftell(fp));
    int i = fread(&b,sizeof(int),1,fp);//将读到的内容放到b变量里面
    printf("b = %d
",b);
    printf("读后文件流指针 = %ld
",ftell(fp));
#endif

    fclose(fp); //关闭文件
    fp = NULL; //防止野指针
    getchar();
    return 0; 
}
文件打开成功
读前文件流指针 = 0
name = 如花
score = 80
age = 12
读后文件流指针 = 29
fseek后文件流指针 = 21
b = 80
读后文件流指针 = 25
//练习,修改学员的年龄,其他信息不修改,通过文件流指针
#include <stdio.h>
#include <string.h>

typedef struct{
    char name[20];
    int score ;
    int age;
} Student;

int main(){

    char c = 'a';
    int  b = 97; 
    Student s;
    strcpy(s.name,"如花");
    s.score = 80;
    s.age= 12;
    Student a[]={0};

    FILE *fp=fopen("day133.txt","r+");
    //文件流指针  在开始的位置
    if(NULL == fp)
    {
        printf("打开文件失败
"); 
        return -1;
    }

    printf("文件打开成功
");
    //练习,修改学员的年龄,其他信息不修改,通过文件流指针
    b = 10;
#if 0
    printf("写前文件流指针 = %ld
",ftell(fp));
    int i =fwrite(&s,sizeof(s),1,fp);
    //文件流指针  在文件末尾
    printf("write i = %d
",i);
    printf("写后文件流指针 = %ld
",ftell(fp));
#endif

#if 1
    fseek(fp,24,SEEK_SET);//设置流文件指针
    printf("fseek后文件流指针 = %ld
",ftell(fp));
    int i =fwrite(&b,sizeof(int),1,fp);
    printf("write i = %d
",i);
    printf("写后文件流指针 = %ld
",ftell(fp));
#endif

    rewind(fp);
    printf("rewind后文件流指针 = %ld
",ftell(fp));
    fread(&a[0],sizeof(s),1,fp);
    printf("name = %s
",a[0].name);
    printf("score = %d
",a[0].score);
    printf("age = %d
",a[0].age);
    printf("读后文件流指针 = %ld
",ftell(fp));
    //关闭文件
    fclose(fp);
    fp = NULL; //防止野指针
    getchar();
    return 0;
}
文件打开成功
fseek后文件流指针 = 24
write i = 1
写后文件流指针 = 29
rewind后文件流指针 = 0
name = 如花
score = 80
age = 10
读后文件流指针 = 29

fprintf
printf --> 将打印信息 写到 标准输出设备(显示器) stdout

参数 比printf,多第一个参数,文件结构体指针

printf  默认输出的文件结构体指针就是stdout
#include <stdio.h>

int main(){
    //fwrite("hello word!",1,12,stdout);
    FILE * fp = fopen("123.txt","w");
    if(!fp)
        //if(fp == NULL)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
    fprintf(fp,"hello world!");
    fclose(fp);
    getchar();
    return 0;
}

#include <stdio.h>
#include <string.h>
typedef struct{
    char name[20];
    int score ;
    int age;
} Student;
int main(){
    //fwrite("hello word!",1,12,stdout);
    Student s;
    strcpy(s.name,"如花");
    s.score = 80;
    s.age= 12;
    Student a[10] = {0};
    FILE * fp = fopen("123.txt","w");
    if(!fp)
        //if(fp == NULL)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
    //fprintf(fp,"hello world!");
    fprintf(fp,"%s,%d,%d",s.name,s.score,s.age);
    fclose(fp);
    getchar();
    return 0;
}

不乱码是因为%d|s格式化成了字符串,所有我们能看到

fscanf
scanf 是从标准输入(键盘 stdin )设备上 读取信息
fscanf 可以从其他文件获得 信息

#include <stdio.h>
#include <string.h>
typedef struct{
    char name[20];
    int score ;
    int age;
} Student;
int main(){
    //fwrite("hello word!",1,12,stdout);
    Student s;
#if 0
    strcpy(s.name,"如花");
    s.score = 80;
    s.age= 12;
    Student a[10] = {0};
#endif

    FILE * fp = fopen("123.txt","r");
    if(!fp)
        //if(fp == NULL)
    {
        printf("打开文件失败
"); 
        return -1;
    }
    printf("文件打开成功
");
    //fprintf(fp,"hello world!");
#if 0
    fprintf(fp,"%s,%d,%d",s.name,s.score,s.age);
#endif
#if 1
    fscanf(fp,"%s%d%d",s.name,&s.score,&s.age);
    printf("s.name = %s
",s.name);
    printf("s.score= %d
",s.score);
    printf("s.age= %d
",s.age);
#endif

    fclose(fp);
    getchar();
    return 0;
}

它会把逗号也看做是一个字符,用空格代替能正常读

    字符的输入输出

    getchar
    int getchar(void); 
    返回从标准输入设备上读到的字符
    putchar
    int putchar(int c);
/*
 * getchar
 * 
 */

#include <stdio.h>

int main(){
    char c = 0;
    c = getchar();
    printf("c = %c
",c);
    getchar();
    getchar();
    return 0;
}

a
c = a

    getc
    int getc(FILE *stream);
    可以对文件中的字符进行读取
    传入 要读取的文件结构体指针

    返回读到的字符
#include <stdio.h>

int main(){
    FILE *fp = fopen("123.txt","r");
    if(!fp)
    {
        printf("文件打开失败
"); 
        return -1;
    }
    char c = 0;
    c = getc(fp);
    printf("c = %c
",c);
    getchar();
    getchar();
    return 0;
}

循环读文件里面的字符

#include <stdio.h>

int main(){
    FILE *fp = fopen("123.txt","r");
    if(!fp)
    {
        printf("文件打开失败
"); 
        return -1;
    }
    char c = 0;
    c = getc(fp);
    while((c = getc(fp)) != EOF)
    {
        printf("c = %c
",c);
    }
    getchar();
    getchar();
    return 0;
}
c = s
c = d
c = s
c = d
c = s
c = d
c = s
c = d
c = s
   putc
    int putc(int c, FILE *stream);
    第一个参数,写入到文件的内容
    第二个参数,要写入文件的结构体指针

/*
* getc
* putc
*
*[练习]
使用上两个函数 实现文件的复制
扩展:带参数main 实现任意文件名的复制
*/

#include<stdio.h>

int main(){

    FILE *fp = fopen("123.txt","r+");
    FILE *fp1 = fopen("abc.txt","w");
    if(!fp)
    {
        printf("文件打开失败
"); 
        return -1;
    }
    char c = 0;
    while((c = getc(fp)) != EOF)
    {
        //printf("c = %c
",c);
        putc(c,fp1);
    }

    return 0;
}

     字符串输入输出  

sprintf
printf 将内容输出到stdout 上
fprintf 将内容输出到文件
sprintf 将内容输出到 string (字符数组)
第一个参数为 字符串 地址 可以是数组 可以动态内存分配得到的内存

#include<stdio.h>

int main(){
    int i = 30;
    char a[20] = {0};
    printf("i= %d
",i);
    sprintf(a,"i = %d",i);
    printf("a =[%s]
",a);
    getchar();
    return 0;
}

/*
 * sprintf
 * 实现strcat
 */
#include<stdio.h>
#include <string.h>
int main(){
    char *str1 = "hello";
    char *str2 = "world";
    char a[20] = {0};
    sprintf(a,"%s",str1);
    sprintf(a+strlen(str1),"%s",str2);
    printf("a =[%s]
",a);
    getchar();
    return 0;
}
a =[helloworld]

动态分配内存

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
    char *str1 = "hello";
    char *str2 = "world";
    char *a = (char *)malloc(20);
    sprintf(a,"%s",str1);
    sprintf(a+strlen(str1),"%s",str2);
    printf("a =[%s]
",a);
    getchar();
    return 0;
}
a =[helloworld]

sscanf

int sscanf(const char *str, const char *format, ..);比scanf多了一个指向字符串的常量指针

将一个字符串 作为 输入

#include<stdio.h>
typedef struct{
    char name[20];
    int score ;
    int age;
} Student;
int main(){
    char *pc = "如花 80 20";
    Student s;    
    sscanf(pc,"%s%d%d",s.name,&s.score,&s.age);
    printf("s.name = %s
",s.name);
    printf("s.score= %d
",s.score);
    printf("s.age  = %d
",s.age);
    getchar();
    return 0;
}
s.name = 如花
s.score= 80
s.age  = 20

scanf() //不能读入有空格 换行符号的字符串
gets() //不安全 容易造成缓存区溢出

      fgets() //可以使用该函数替代上面的函数
    char *fgets(char *s, int size, FILE *stream);
    可以从stream 文件 中 读取指定字节数 size 到s所指向的内存

    会读入size -1 个字符 最后一个空间是0
    如果录入的个数size-1  会把
 放到buf中
    如果大于size 多余的 字符会在输入缓冲区,需要请缓冲
#include<stdio.h>
#include <stdlib.h>
int main(){
    char *buf = (char *)malloc(30);
    if(!buf)
    {
        printf("内存分配失败
"); 
        return -1;
    }
    FILE *fp = fopen("123.txt","r");
    if(!fp) 
    {
        printf("打开文件失败
"); 
        return -1;
    }
    fgets(buf,13,fp);  //从文件读取
    printf("buf = %s
",buf);
    fclose(fp);
    free(buf);
    getchar();
    return 0;
}
//标准输入设备
#include<stdio.h>
#include <stdlib.h>
int main(){
    char *buf = (char *)malloc(30);
    if(!buf)
    {
        printf("内存分配失败
"); 
        return -1;
    }
    FILE *fp = fopen("123.txt","r");
    if(!fp) 
    {
        printf("打开文件失败
"); 
        return -1;
    }
    fgets(buf,8,stdin);  //标准输入设备
    printf("buf = %s
",buf);
    fclose(fp);
    free(buf);
    getchar();
    getchar();
    return 0;
}
 会读入size -1 个字符 最后一个空间是
  如果录入的个数size-1  会把
 放到buf中
    如果大于size 多余的 字符会在输入缓冲区,需要请缓冲

int fclose(FILE *stream);
头文件 #include <stdio.h>
关闭文件,传入要关闭的文件结构体指针(fopen 函数的返回值)
成功返回 0 失败返回 EOF

何所为,不仅仅是一种态度,更是一种艺术!
原文地址:https://www.cnblogs.com/tanzizheng/p/12927418.html