#include<iostream>
#include
<string.h>

using namespace std; //申明命名空间

//error()函数
void error()
{
cout
<<"/nCan not open the file."<<endl;
exit(
1);//退出
}

int main(void)
{
FILE
*fp;//定义一个文件指针
char str[80];

cout
<< "Input:";//从键盘读取输入
cin.getline(str,80);

if( (fp=fopen("test.dat","w")) == NULL) //新建一个test.dat文件
error();

fputs(str,fp);
//write the str to fp;
fputs("\n",fp);
fclose(fp);
//Close the file;

if((fp=fopen("test.dat","r"))==NULL)
error();

char ch;
cout
<<"Output:";
while( (ch=fgetc(fp)) != EOF)
cout
<< ch;
fclose(fp);
}

一个十分简单的例程.

相关文章:

  • 2021-11-15
  • 2021-09-13
  • 2022-01-18
  • 2021-11-21
  • 2021-10-09
  • 2021-10-07
  • 2022-12-23
猜你喜欢
  • 2021-06-21
  • 2021-09-14
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
相关资源
相似解决方案