c语言文件操作函数及实例

文件三步曲:

  • 打开
  • 读写
  • 关闭

//
//  main.c
// 文件操作
//  Created by fzl 
//  Copyright ©  All rights reserved.
//

#include <stdio.h>

/**
 *  fputc()
 */
void test1()
{
    //1.打开文件
    /*
     参数1:打开文件的路径,绝对路径,相对路径
     参数2:打开的方式,

     */
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w");
    if(fp==NULL)
    {
        perror("fopen failed");
    }
    //2.对文件进行操作(读/写)

    char ch='a';
    fputc(ch, fp);
    //3.关闭文件

    fclose(fp);


}

/**
 *  fgetc()
 */
void test2()
{
    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r");
    if(fp==NULL)
    {
        perror("fopen failed");
    }

    //2.
    char ch=0;

    ch=fgetc(fp);
    printf("ch=%c\n",ch);

    //
    fclose(fp);


}
/**
 *  fprintf()
 */
void test3()
{
    int a=8;
    char ch='a';
    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w");
    if(fp==NULL)
    {
        perror("fopen failed");
    }
    //2.
    fprintf(fp, "a=%d,ch=%c\n",a,ch);
    //3.
    fclose(fp);


}

/**
 *fscanf()
 */
void test4()
{
    int a=0;
    char ch=0;
    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r");
    if(fp==NULL)
    {
        perror("fopen failed");
    }
    //2.
    fscanf(fp, "a=%d,ch=%c\n",&a,&ch);
    printf("a=%d\nch=%c\n",a,ch);
    //3.
    fclose(fp);


}

/**
 *  fputs()
 */
void test5()
{
    char name[20]="rose";

    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w");
    if(fp==NULL)
    {
        perror("fopen failed");
    }

    //2.
    fputs(name, fp);

    //3.
    fclose(fp);

}
/**
 *  fgets()
 */
void test6()
{
    char name[20];

    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r");
    if(fp==NULL)
    {
        perror("fopen failed");
    }

    //2.
    fgets(name, sizeof(name), fp);
    printf("name=%s",name);

    //3.
    fclose(fp);
}
/**
fseek(),ftell()
 */
void test7()
{
    char ch;

    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r");
    if(fp==NULL)
    {
        perror("fopen failed");
    }

    fseek(fp, 0, SEEK_END);

    printf("文件大小=%ld\n",ftell(fp));
    //2.
    ch=fgetc(fp);
    printf("ch=%c\n",ch);

    //3.
    fclose(fp);


}

void test8()
{
    typedef struct Student
    {
        int age;
        char name[20];
    }Student;


    Student stu={10,"rose"};
    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w");
    if(fp==NULL)
    {
        perror("fopen failed");
    }

    //2.
    fwrite(&stu, sizeof(stu), 1, fp);

    //3.
    fclose(fp);


}
/**
 *  fread()
 */
void test9()
{
    typedef struct Student
    {
        int age;
        char name[20];
    }Student;

    Student stu;
    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r");
    if(fp==NULL)
    {
        perror("fopen failed");
    }

    //2.
    fread(&stu, sizeof(stu), 1, fp);

    printf("stu的name=%s,age=%d\n",stu.name,stu.age);

    //3.
    fclose(fp);

}

void test10()
{
    typedef struct Student
    {
        int age;
        char name[20];
    }Student;

    Student stu[3]={
        {10,"rose"},
        {20,"jack"},
        {30,"white"}
    };
    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w");
    if(fp==NULL)
    {
        perror("fopen failed");
    }

    //2.
    //fwrite(stu, sizeof(stu), 1, fp);
    int i=0;
    for (; i<3; i++)
    {
        fwrite(&stu[i], sizeof(Student), 1, fp);
    }


    //3.
    fclose(fp);



}

void test11()
{
    typedef struct Student
    {
        int age;
        char name[20];
    }Student;

    Student stu[3];
    //1.
    FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r");
    if(fp==NULL)
    {
        perror("fopen failed");
    }

    //2.
    //fread(stu, sizeof(Student), 3, fp);
    int i=0;
    for (; i<3; i++)
    {

        fread(&stu[i], sizeof(Student), 1, fp);
        printf("stu[%d].name=%s,age=%d\n",i,stu[i].name,stu[i].age);
    }

    //3.
    fclose(fp);


}
int main()
{

    test11();
    return 0;
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值