1.写数据到一个txt文件
2.写数据到多个txt文件
#include
#include
#include
using namespace std;
int main()
{
char filePath[128];
sprintf(filePath, "d:data\a.txt"); //要用双"\",不要用单"\"
ofstream outfile;
outfile.open(filePath);
outfile << "C++" << endl;
outfile.close();
return 0;
}
2.写数据到多个txt文件
#include
#include
#include
using namespace std;
int main() {
char filePath[128]; //为filePath配空间
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
sprintf(filePath, "d:data\%d.txt", i); //命名为"0.txt", "1.txt", …
ofstream outfile;
outfile.open(filePath);
outfile << "C++" << endl;
outfile.close();
}
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)