读写文件--c

3 篇文章 0 订阅

读文件

一,

string line;
ifstream in;
  in.open(filepath);  
  if(! in){   
   return;
  }
  
  CString str;
  CString strtemp;
   while (getline(in,line)){
   str = line.c_str();
   strtemp = str;
    }
  in.close();

二,

#include <stdio.h>

void main( void )
{
   FILE *stream;
   char list[30];
   int  i, numread, numwritten;

   if( (stream = fopen( "fread.txt", "r+t" )) != NULL )
   {
      /* Attempt to read in 25 characters */
      numread = fread( list, sizeof( char ), 25, stream );
      printf( "Number of items read = %d\n", numread );
      printf( "Contents of buffer = %.25s\n", list );
      fclose( stream );
   }
   else
      printf( "File could not be opened\n" );
}

写文件

一,

ofstream m_modelout;
  
  m_modelout.open(originmodelfile,ios::out);
  if(! m_modelout)
  {
   cout<< "could not Open File :"<<originmodelfile<<endl;
   return ;
  }
  //m_modelout.seekp(0,ios::beg);

m_modelout.seekp(0,ios::end);
m_modelout.write(str,str.GetLength());
m_modelout.write("\n",1);

 

二,
#include <stdio.h>

void main( void )
{
   FILE *stream;
   char list[30];
   int  i, numread, numwritten;

   /* Open file in text mode: */
   if( (stream = fopen( "fread.txt", "w+t" )) != NULL )
   {
      for ( i = 0; i < 25; i++ )
         list[i] = (char)('z' - i);
      /* Write 25 characters to stream */
      numwritten = fwrite( list, sizeof( char ), 25, stream );
      printf( "Wrote %d items\n", numwritten );
      fclose( stream );

   }
   else
      printf( "Problem opening the file\n" );

   }

 

三,

FILE *out;
if ((out = fopen(ofname, "w")) == NULL) {
 fprintf(stderr, "The file %s can't be opened for writing.\n", ofname);
        exit(1);
}

fprintf(out, "%s", "dfs");


 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值