读写文件--c

来源:互联网 发布:炸鸡叔 绝命毒师 知乎 编辑:程序博客网 时间:2024/06/05 15:38

读文件

一,

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");


 

 

 

 

 

 

 

 

 

 

原创粉丝点击