2021.12.27小技巧积累
1.如何在C++中实现类似于printf("Hello World! %d",i)这种int型转char(还是传值的这种)?
char text[32]={};
int i=0;
sprintf(text,"这是数字:%d",i);
例子:实现一个动态输出箭头个数的代码
static int arrowNumber=0;
staic char arrowText[32]={};
++arrowNumber;
sprintf(arrowText,"这是第%d个箭头",arrowNumber);
std::cout<<arrowText<<std::endl;
(外部搞了个for循环模拟业务逻辑)
现象输出:
Tips:不要和C语言中的printf记混了,这个不是输出到控制台!只是将那个int型的数字放入到那个char型数组中!
2.windows和QT下的文件操作
Windows文件操作:
需要头文件:
#include <fstream>
std::fstream out;
out.open("G:/test.txt");
out<<"HelloWorld!"<<std::endl;
out.close() ;
QT文件操作:
需要头文件:
#include <QFile>
#include <QTextStream>
QFile file("G:/123.txt");
file.open(QFile::ReadWrite|QIODevice::Text); //ReadWrite是读写模式,Text我目前知道的是加了这个可以用QTextStream的endl换行方法了。
QTextStream test(&file);
test<<"hhhhhhhhhhhhhhh"<<endl;
file.close();
Tips:QTextStream中的换行不用加命名空间哦!直接用就行!!!