流文件的打开和关闭.cpp

来自「为filename 所指定的文件名按mode 模式创建一个FILE结构数据区,并」· C++ 代码 · 共 38 行

CPP
38
字号
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>

main()
{
	FILE *fp1;
	char str[80];

	cout<<"Input a string:";
	cin.getline(str.80);

	if((fp1=fopen("d.dat","w"))==NULL)
	{
		cout<<"\nCould not open the file."<<endl;
		cout<<"Exiting program."<<endl;
		exit(1);
	}
	fputs(str,fp1);
	fputs("\n",fp1);

	fclose(fp1);

	if((fp1=fopen("d.dat","r"))==NULL)
	{
		cout<<"\nCould not open the file ."<<endl;
		cout<<"Exiting program."<<endl;
		exit(1);
	}

	char ch;
	while ((ch=fgetc(fp1)!=EOF)
		cout<<ch<<endl;
	fclose(fp1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?