This is an old revision of the document!
C++
I/O
#include <fiostream.h> int main () { ofstream out("out.txt"); if (out.is_open()) { out << "This is a line.\n"; 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;
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); }
将变量名替换为字符串
#define name2str(name) (#name)
#define xstr(s) str(s) #define str(s) #s #define foo 4 str (foo) ==> "foo" xstr (foo) ==> xstr (4) ==> str (4) ==> "4"
函数指针作为参数
void DiffusionModel (bool (*GeometryFunction)(double, double, double)) ) bool InsideAerogel(double x, double y, double z); DiffusionModel( InsideAerogel );
char * 转换 (for input arg.)
//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;