Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
soft:c [2020/07/24 08:23] Ce Zhangsoft:c [2020/11/05 02:10] (current) Ce Zhang
Line 1: Line 1:
-====C++==== +=====C++=====
-===Read===+
  
-''Monospaced Text''+==== 2D array ==== 
 +<code> 
 +TGraph*** g = new TGraph**[Nfile]; 
 +for(i loop) g[i] = new TGraph*[t[i]->Nentries]; 
 +for(int j = 0; j<t[i]->Nentries;j++) g[i][j] = ...(TGraph *)... 
 +</code>
  
-#include <fstream+====EOF==== 
-  * ofstream        create, write into new file +<code> 
-  * ifstream         read the existing file +<<EOF        //开始 
-  * fstream          read & write+.... 
 +EOF            //结束 
 + 
 +//将一个文件的内容输出到另一个文件中: 
 +cat fileA > fileB 
 +//将"<< EOF EOF"替代输入对象文件fileA: 
 +# cat << EOF fileB 
 +//命令执行后,提示用户输入内容,输入结束后,用户的输入内容被保存到了fileB中。 
 +</code> 
 + 
 +====I/O====
  
 <code> <code>
Line 16: Line 30:
        {        {
             out << "This is a line.\n";             out << "This is a line.\n";
-            out << "This is another line.\n"; 
             out.close();             out.close();
 +        }
 +      return 0;
 +      
 + // reading a text file
 +    #include <iostream.h>
 +    #include <fstream.h>
 +    #include <stdlib.h>
 +    
 +    int main () {
 +        char buffer[256];
 +        ifstream in("test.txt");
 +        if (! in.is_open())
 +        { cout << "Error opening file"; exit (1); }
 +        while (!in.eof() )
 +        {
 +            in.getline (buffer,100);
 +            cout << buffer << endl;
         }         }
         return 0;         return 0;
Line 23: Line 53:
 </code> </code>
  
 +
 +<code>
 +std::vector<char> v;
 +
 + if (FILE *fp = fopen("filename", "r"))
 + {
 + char buf[1024];
 + while (size_t len = fread(buf, 1, sizeof(buf), fp))
 + v.insert(v.end(), buf, buf + len);
 + fclose(fp);
 + }
 +</code>
 +====将变量名替换为字符串====
 +<code>
 +#define name2str(name) (#name)
 +</code>
 +
 +<code>    
 +#define xstr(s) str(s)
 +#define str(s) #s
 +#define foo 4
 +
 +     str (foo)
 +          ==> "foo"
 +     xstr (foo)
 +          ==> xstr (4)
 +          ==> str (4)
 +          ==> "4"
 + </code>
 +
 +====函数指针作为参数====
 +<code>
 +
 +void DiffusionModel (bool (*GeometryFunction)(double, double, double)) )
 +bool InsideAerogel(double x, double y, double z);
 +
 +DiffusionModel(
 +    InsideAerogel
 +);
 +</code>
 +
 +====char * 转换 (for input arg.)====
 +<code>
 +//char* to int
 +
 +int a = stoi(argv[1]);
 + 
 +//char* 转 float/double
 +float b = stof(argv[2]);
 +double bb = stod(argv[2]);
 + 
 +//char* 转 string (可以直接转)
 +string str = argv[3];
 +cout << a << endl << b << endl << bb << endl << str << endl;
 +</code>