Эй Я пытаюсь записать некоторые цифры в файл, но когда я открываю файл, он пуст. Вы можете мне помочь? Благодарю.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 **/
}
Просто взгляд показывает, что этот код не будет скомпилирован - массив «random_array» объявлен с непостоянной переменной для своей длины. Вы должны опубликовать свой фактический код, я подозреваю, что вы упростили –
@Terry: это нестандартное расширение. – Potatoswatter
@ Potatocorn: Действительно? Боже мой, на какой платформе? – Cameron