fread与fwrite

1、fread

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int a;
    FILE* pFile = fopen("C:\\Users\\lucky\\Desktop\\22.txt", "r");
    char str[1024] = { 0 };
    while (a = fread(str, sizeof(char), sizeof(str), pFile))//接受信息的数组的首地址,接受信息的数组的类型的大小(每次需要读字符的大小),接受数组的大小,输入文件的地址
    {
        printf(str);
    }
    fclose(pFile);
    system("pause");
    return 0;
}//while中的判断语句也可以改为feof(!pFile):作用是判断是不是文件是否到结尾了。到结尾返回1,没到结尾返回0.

2、fwrite

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    /*int a;*/
    FILE* pFile = fopen("C:\\Users\\lucky\\Desktop\\22.txt", "w");//接着写a,擦除写w,r与r+是写入数据。
    char* str = "hello b0\n";
    char* str1 = "hello b1\n";
    char* str2= "hello b2";
    fwrite(str,sizeof(char),strlen(str),pFile);
    fwrite(str1,sizeof(char),strlen(str1),pFile);
    fwrite(str2,sizeof(char),strlen(str2),pFile);
    /*a=fwrite(str,sizeof(char),strlen(str),pFile);
    a = errno;*/

    fclose(pFile);
    /*FILE* PFILE = NULL;
    errno_t a = fopen_s(&PFILE, "C:\\Users\\lucky\\Desktop\\111.txt", "r");*/
    //printf("%d\n", a);
    
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

clown_30

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值