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;
}
569

被折叠的 条评论
为什么被折叠?



