文件的基本操作 C语言版

来源:互联网 发布:网络授权 编辑:程序博客网 时间:2024/06/08 19:42
 文件的写操作:

void CMYCFileView::OnFileWrite()
{
 // TODO: Add your command handler code here
 //打开文件
 FILE *pFile = fopen("111.txt","w");
 //写入操作
 fwrite("
http://www.baidu.com",1,strlen("http://www.baidu.com"),pFile);
 //控制文件指针位置
 fseek(pFile,0,SEEK_SET);
 //接着上次的位置继续写
 fwrite("欢迎访问",1,strlen("欢迎访问"),pFile);
 //关闭文件
 //fclose(pFile);
 //将缓冲区中的数据写入到磁盘文件中
 fflush(pFile);

  }

 

读出文件:--读取文件内容时,应正确地设置文件指针的位置。

void CMYCFileView::OnFileRead()
{
 // TODO: Add your command handler code here
  FILE *pFile = fopen("111.txt","r");
  char ch[100];
  fread(ch,1,100,pFile);
  fclose(pFile);

//将文件读到字符数组中并显示
  MessageBox(ch);

 
}

 

C++ 对文件的操作:

读出文件:

ifstream ifs("4.txt");
char ch[100];
memset(ch,0,100);
ifs.read(ch,100);
ifs.close();
MessageBox(ch);

写入文件:

ofstream ofs("4.txt");
ofs.write("http://www.baidu.com",strlen("http://www.baidu.com"));
ofs.close();

原创粉丝点击