C语言
sprintf(数字转字符串)
char name[10];
sprintf(name, "%d", 100);
atoi和atof(分别转换为int和double)
cout << atoi("100") << endl;
cout << atof("100.03") << endl;
C++使用stringstream即可
需要头文件 #include <sstream>
- 下面这个例子是将字符串转为int类型的,
stringstream s;
s << "10";
int a;
s >> a;
cout << a << endl;
- 下面这个例子是将int类型转为字符串的例子
stringstream s;
s << 10;
int a;
char name[10];
s >> name;
cout << name << endl;
cout << strlen(name) << endl;
- 下面这个例子是将字符串转为double的。
stringstream s;
s << "10.20";
double a;
s >> a;
cout << a << endl;
- 下面这个例子是将double转为字符串的操作。
stringstream s;
s << 10.20;
int a;
char name[10];
s >> name;
cout << name << endl;
cout << strlen(name) << endl;
C++文件操作
可以直接使用重载的操作符<<,也可以使用write函数进行操作。
注意:不要使用取消转义字符的R 语法来
string str2 =R"( "C:\Program Files\QQ\Bin\QQ.exe")";
ofstream fs("C:\\Users\\ytkj\\Desktop\\新建文本文档 (5).txt");
int a[] = { 1,2,34 };
copy(a, a + 3, ostream_iterator<int>(fs, ","));
fs.write("fsdfdfd",10);
fs.close();