안녕하세요 저는 파일에 숫자를 쓰려고하는데, 파일을 열면 비어 있습니다. 여기서 나를 도울 수 있니? 감사.C++ forstream은 파일에 쓸 수 없습니다.
/** 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"배열은 길이가 아닌 변수로 선언됩니다. 실제 코드를 게시해야합니다. 단순화 된 것으로 의심됩니다. –
@ 테리 : 비표준 확장입니다. – Potatoswatter
@Potatocorn : 정말요? 좋은 천국, 어떤 플랫폼? – Cameron