【发布时间】:2013-12-02 01:47:50
【问题描述】:
我有这段代码,我正在努力工作(不正确)现在它会创建一个大文件,但我希望它生成一系列随机标题的文件。
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
string random(int len)
{
string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string r;
srand(time(NULL));
for(int i = 0; i < len; i++) r.push_back(a.at(size_t(rand() % 62)));
return r;
}
int main(){
std::ofstream o("largefile.txt");
o << random(999) << std::endl;
return 0;
}
我尝试添加此内容,但在 std::ofstream 中收到有关数据类型的错误
std::string file=random(1);
std::ofstream o(file);
【问题讨论】:
-
如果你使用的是c++03,那么你必须使用
std::ofstream o(file.c_str()),因为ofstream在c++11之前没有使用string的构造函数,只有@987654327 @. -
在我的 gcc 版本 4.6.3 上编译得很好。
标签: c++ file random compiler-errors ofstream