C++ ofstream 无法写入文件
嘿,我正在尝试将一些数字写入文件,但是当我打开文件时它是空的。你能帮我一下吗?谢谢。
/** main function **/
int main(){
/** variables **/
RandGen* random_generator = new RandGen;
int random_numbers;
string file_name;
/** ask user for quantity of random number to produce **/
cout << "How many random number would you like to create?" << endl;
cin >> random_numbers;
/** ask user for the name of the file to store the numbers **/
cout << "Enter name of file to store random number" << endl;
cin >> file_name;
/** now create array to store the number **/
int random_array [random_numbers];
/** file the array with random integers **/
for(int i=0; i<random_numbers; i++){
random_array[i] = random_generator -> randInt(-20, 20);
cout << random_array[i] << endl;
}
/** open file and write contents of random array **/
const char* file = file_name.c_str();
ofstream File(file);
/** write contents to the file **/
for(int i=0; i<random_numbers; i++){
File << random_array[i] << endl;
}
/** close the file **/
File.close();
return 0;
/** END OF PROGRAM **/
}
Hey I am trying to write some numbers to a file, but when I open the file it is empty. Can you help me out here? Thanks.
/** main function **/
int main(){
/** variables **/
RandGen* random_generator = new RandGen;
int random_numbers;
string file_name;
/** ask user for quantity of random number to produce **/
cout << "How many random number would you like to create?" << endl;
cin >> random_numbers;
/** ask user for the name of the file to store the numbers **/
cout << "Enter name of file to store random number" << endl;
cin >> file_name;
/** now create array to store the number **/
int random_array [random_numbers];
/** file the array with random integers **/
for(int i=0; i<random_numbers; i++){
random_array[i] = random_generator -> randInt(-20, 20);
cout << random_array[i] << endl;
}
/** open file and write contents of random array **/
const char* file = file_name.c_str();
ofstream File(file);
/** write contents to the file **/
for(int i=0; i<random_numbers; i++){
File << random_array[i] << endl;
}
/** close the file **/
File.close();
return 0;
/** END OF PROGRAM **/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能在堆栈上声明一个大小仅在运行时已知的整数数组。但是,您可以在堆上声明这样的数组:
不要忘记在 main() 末尾添加
delete [] random_array;
(以及delete random_generator;
)释放使用new
分配的内存。当您的程序退出时,该内存会自动释放,但无论如何释放它都是一个好主意(如果您的程序不断增长,很容易忘记稍后添加它)。除此之外,你的代码看起来不错。
You can't declare an array of integers with a size known only at run-time on the stack. You can declare such an array on the heap however:
Don't forget to add
delete [] random_array;
at the end of main() (anddelete random_generator;
too) to deallocate the memory that you allocated usingnew
. This memory is automatically freed when your program exits, but it's a good idea to release it anyway (if your program ever grows, it's easy to forget to add it in later).Apart from that, your code looks fine.
无需循环两次或保留数组或向量。
There's no need to loop twice or keep an array or vector.
如果我只是填写您的 RandGen 类来调用
rand
,该程序在 Mac OS X 10.6 上运行良好。此外,我认为它没有理由不在 GCC 上工作。您运行在什么版本和平台上?能提供完整源码吗?
If I just fill in your RandGen class to call
rand
, the program works fine on Mac OS X 10.6.Moreover, I see no reason for it not to work on GCC. What version and platform are you running on? Can you provide complete source?