파일에서 내용을 삭제하려고합니다. 지금까지 다른 임시 파일에서 삭제하지 않은 모든 데이터를 가져 와서 가장 오래된 파일을 삭제하는 아이디어에 도달했습니다. 여기 내 코드의 임시 이름을 바꿉니다어떤 것이 작동하지 않습니다.
string username;
cout << "Enter The Employee you want to Remove : ";
cin >> username;
int i=0;
string line1,line2,line3;
fstream myfile;
fstream temp;
myfile.open("Data.txt");
temp.open("temp.txt");
if (myfile.is_open() || temp.is_open())
{
while (myfile >> line1 >> line2 >> line3)
{
if(line1!="Employee" || username!=line2)
{
temp << line1 << endl << line2 << endl << line3 << endl;
continue;
}
else if (line1=="Employee" && username==line2)
{
i=1;
}
}
myfile.clear();
myfile.seekg(0, ios::beg);
myfile.close();
temp.close();
remove("Date.txt");
rename("temp.txt","Date.txt");
if(i==0)
{
cout << "There is no Employee with The Name you Entered!" << endl;
}
else
{
cout << "Employee data has been Deleted." << endl;
}
}
나는 코드 내 자신을 추적하며 임시 파일을 생성하지 않습니다! 이 기술을 사용하여 업데이트 할 수 있습니까?
내가 수동으로 실행하는 임시 파일을 만들 때 모양이 하지만 가장 오래된 것을 삭제하지 않습니다 !! –