• 文件操作


    C文件操作:

    fopen

    /* fopen example */
    #include <stdio.h>
    int main ()
    {
      FILE * pFile;
      pFile = fopen ("myfile.txt","w");
      if (pFile!=NULL)
      {
        fputs ("fopen example",pFile);
        fclose (pFile);
      }
      return 0;
    }

    fprintf

    /* fprintf example */
    #include <stdio.h>
    int main ()
    {
       FILE * pFile;
       int n;
       char name [100];
    
       pFile = fopen ("myfile.txt","w");
       for (n=0 ; n<3 ; n++)
       {
         puts ("please, enter a name: ");
         gets (name);
         fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
       }
       fclose (pFile);
    
       return 0;
    }

    sprintf

    /* sprintf example */
    #include <stdio.h>
    
    int main ()
    {
      char buffer [50];
      int n, a=5, b=3;
      n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
      printf ("[%s] is a %d char long string\n",buffer,n);
      return 0;
    }

    sscanf

    /* sscanf example */
    #include <stdio.h>
    
    int main ()
    {
      char sentence []="Rudolph is 12 years old";
      char str [20];
      int i;
    
      sscanf (sentence,"%s %*s %d",str,&i);
      printf ("%s -> %d\n",str,i);
      
      return 0;
    }

    C++文件操作:

    ostream::write

    // Copy a file
    #include <fstream>
    using namespace std;
    
    int main () {
    
      char * buffer;
      long size;
    
      ifstream infile ("test.txt",ifstream::binary);
      ofstream outfile ("new.txt",ofstream::binary);
    
      // get size of file
      infile.seekg(0,ifstream::end);
      size=infile.tellg();
      infile.seekg(0);
    
      // allocate memory for file content
      buffer = new char [size];
    
      // read content of infile
      infile.read (buffer,size);
    
      // write to outfile
      outfile.write (buffer,size);
      
      // release dynamically-allocated memory
      delete[] buffer;
    
      outfile.close();
      infile.close();
      return 0;
    }

    istream::read

    // read a file into memory
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
      int length;
      char * buffer;
    
      ifstream is;
      is.open ("test.txt", ios::binary );
    
      // get length of file:
      is.seekg (0, ios::end);
      length = is.tellg();
      is.seekg (0, ios::beg);
    
      // allocate memory:
      buffer = new char [length];
    
      // read data as a block:
      is.read (buffer,length);
      is.close();
    
      cout.write (buffer,length);
    
      delete[] buffer;
      return 0;
    }
  • 相关阅读:
    jsTree获取选中节点和选中指定节点
    jsTree的checkbox默认选中和隐藏
    ElasticSearch 报错 failed to obtain node locks
    jstree单选功能的实现方法
    layui 报错 jQuery is not defined
    ssdb make 失败 autoconf required
    No package gcc48-c++ available
    查看yum已安装的包
    PHP 生成 UUID
    [构造][dfs树][树的重心][LOJ#3176]「IOI2019」景点划分
  • 原文地址:https://www.cnblogs.com/pure/p/2480325.html
Copyright © 2020-2023  润新知