C语言的文件笔记

文件合成器:将两个文件以二进制的形式读出,再将其写入到新文件中。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *f_pic,*f_file,*f_finish;
    char pic_name[20],file_name[20],file_finish[20];
    char ch;

    printf("请输入图片名和文件名:\n");

    printf("图片:");
    scanf("%s",pic_name);

    printf("文件:");
    scanf("%s",file_name);

    printf("请输入生成文件名:");
    scanf("%s",file_finish);

    if(!(f_pic = fopen(pic_name,"rb")))
    {
        printf("Cannot open the picture %s",pic_name);
        return 0;
    }
      if(!(f_file = fopen(file_name,"rb")))
    {
        printf("Cannot open the file_name %s",file_name);
        return 0;
    }
    if(!(f_finish = fopen(file_finish,"wb")))
    {
        printf("Cannot open the file_finish %s",file_finish);
        return 0;
    }
    while(!feof(f_pic))
    {
        ch = fgetc(f_pic);
        fputc(ch,f_finish);
    }
    fclose(f_pic);
    while(!feof(f_file))
    {
        ch  = fgetc(f_file);
        fputc(ch,f_finish);
    }
    fclose(f_file);
    fclose(f_finish);

    system("pause");
    return 0;
}

从指定文件读出数据,写到工作台

#include <stdio.h>
#include <stdlib.h>

#define LEN 31
int  main()
{
    FILE *fp;
    char buffer[LEN];

    if(!(fp = fopen("F://2.txt","rt")))//2.txt,F盘中的txt文件
    {
        printf("Cannot open the file ");
        exit(0);
    }
    fgets(buffer,LEN,fp);

    printf("the content are %s",buffer);
    return 0;
}

将数据写入指定的文件中,以及从指定文件读出数据

#include <stdio.h>
#include <stdlib.h>
#define LEN 100
int main()
{
    char ch,buffer[LEN];
    FILE *fp;
    if(!(fp = fopen("F:\\2.txt","at+")))
    {
        printf("Cannot open the file %s","F:\\2.txt");
    }
    printf("Please input a string :\n");

    fgets(buffer,LEN,stdin); //键盘就是一个输入流,键盘上输入的数据存在stdin中

    fputs(buffer,fp);

    rewind(fp); //将fp指针,指向文件中的第一个字符

    //读取fp所指向文件的数据
    ch = fgetc(fp);
    while( ch != EOF)
    {
        putchar(ch);
        ch = fgetc(fp);
    }
    fclose(fp);
    return 0;
}

fread()与fwrite()的使用

#include <stdio.h>

#define LEN 4

void save();
void load();

struct Student
{
    char name[20];
    int num;
    int age;
    char addr[30];
}stu[LEN];
int main()
{
    int i =0 ;
    printf("请输入学生的信息:\n");
    for(i = 0;i<LEN;i++)
    {
        scanf("%s %d %d %s",stu[i].name,&stu[i].num,&stu[i].age,stu[i].addr);
    }
    save();
    load();
    return 0;
}
void save()
{
    int i = 0;
    FILE *fp;
    if(!(fp = fopen("F://information","wb")))
    {
        printf("Cannot open the txt %s ","F://2.txt");
    }
    for(i=0;i<LEN;i++)
    {
        fwrite(&stu[i],sizeof(struct Student),1,fp);
    }
    fclose(fp);

}
void load()
{
    struct Student student[LEN];
    int i = 0;
    FILE *fp;
    char ch;
    if(!(fp = fopen("F://information","rb")))
    {
        printf("Cannot open the txt %s ","F://2.txt");
    }
    for(i=0;i<LEN;i++)
    {
        fread(&student[i],sizeof(struct Student),1,fp);
    }

    fclose(fp);

    printf("学生信息为:\n");
    for(i=0;i<LEN;i++)
    {
        printf("%10s %5d %5d  %10s \n",student[i].name,student[i].num,student[i].age,student[i].addr);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值