Code
#include <iostream>
#include <cstdlib>
#include <process.h>
FILE *stream, *stream1, *stream2;
#pragma warning(disable:4996)
int main()
{
int numclosed;
char list[30]; // 存放从文件中读取的数据
int i, numread, numwritten;
// 打开文件 data 进行度,文件不存在则失败
if (!(stream1 = fopen("data", "r")))
printf("Data Failed !\n");
else
printf("Data Successful !\n");
// 打开文件2进行写操作
if (!(stream2 = fopen("data2", "w+")))
printf("Data2 Write Failed !\n");
else
printf("Data2 Write Successful !\n");
// 使用文本模式打开文件,对文件进行写操作
if ((stream = fopen("fread.out", "w+t")))
{
// 向文件流中写入25个字符
for (i = 0; i < 25; i++)
{
list[i] = char('z' - i);
}
numwritten = fwrite(list, sizeof(char), 25, stream);
printf("Written Successful: %d\n", numwritten);
fclose(stream);
}
else
{
printf("fread.out Write Failed !\n");
}
if ((stream = fopen("fread.out", "r")))
{
numread = fread(list, sizeof(char), 25, stream);
printf("读取的数据个数 = %d\n", numread);
printf("读取的内容为:%.25s\n", list);
fclose(stream);
}
else
{
printf("fread.out Read Failed !\n");
}
numclosed = _fcloseall();
printf("Closed File Number is: %d\n", numclosed);
system("pause");
return 0;
}
Output