0
아무에게도 다음과 같은 코드가 예기치 않은 런타임 오류가 발생하는 이유를 알려주십시오. 그것은 두 번 반복하지만 그 이상은 작동하지 않습니다.간단한 코드로 예기치 않은 런타임 오류가 발생했습니다.
는#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void print(string hmm)
{
ofstream ko(hmm.c_str(),ios::trunc);
ko<<"chacho";
ko.close();
}
int main(){
for(int i=0;i<5;i++)
{
char *chat=new char;
sprintf(chat,"%d%s",i,"_num.txt");
string rat=chat;
print(rat);
}
system("pause");
return 0;
}
어떤 오류가 발생합니까? –
#define BUF_LEN (256) ...... char * chat = new char [BUF_LEN]; ...... dlete [] 채팅; – neohope
@neohope, define은이 목적에 전혀 적합하지 않습니다. C++에서 그렇게하는 많은 좋은 방법이 있습니다. 또한 새로운 대괄호가 필요한 배열을 할당합니다. – chris