현재 4 개의 다른 변수가 있으며 다음 형식으로 저장됩니다.
아티스트 : 제목 : 연도 : 카테고리
현재 파일을 열고 해당 변수에 저장 중이며 getline을 사용해 보았지만 해당 변수를 모두 삭제할 수 없습니다.
void deletesong(){
string oldfile;
ifstream openFile;
ofstream ofile;
string line;
string songnme;
string oartist[NUM], otitle[NUM], oyear[NUM], ocategory[NUM];
int counter;
bool finddvd;
//User enters filename, add .txt to make sure its saved as filename.txt
do{
cout << "Please enter the name of the file you would like to delete from: ";
cin >> oldfile;
oldfile += ".txt";
openFile.open(oldfile.c_str());
if(openFile.fail()){
cerr << "Check spelling of file name.\n";
error = true;
}
//Storing text from file into arrays
}while(error == true);
while(getline( openFile, oartist[counter], ':') && getline( openFile, otitle[counter], ':') &&
getline( openFile, oyear[counter], ':') && getline( openFile, ocategory[counter])){
counter++;
}
cout << "Enter the name of the song you would like to delete: ";
cin >> songname;
이것은 파일 이름을 검색하고 파일을 열고 모든 것을 배열에 저장하는 데 사용하는 코드입니다.
내 코드가 필요한 것은 예를 들어 누군가 Ruby의 노래 이름을 삭제하려는 경우 텍스트 파일에 다음과 같이 표시됩니다.
xxxx : 루비 : xxxx : xxxx
줄의 모든 정보를 하나의 레코드로 삭제하려면 프로그램이 필요합니다.
std::vector<std::string>
.NET과 같은 문자열 배열을 선언 하는 대신 사용하고 싶습니다 string oartist[NUM]
.
또한 데이터를 하나의 구조로 구성 할 수 있습니다. 예를 들면 :
struct record
{
std::string artist, title, year, category;
};
std::vector::push_back
데이터를 추가하는 데 사용 합니다. 그런 다음 벡터 중간에있는 레코드를 제거하거나 벡터에 새 데이터를 추가 할 수 있습니다. 마지막으로 데이터를 저장하십시오. 예를 들면 :
std::fstream openFile(filename, std::ios::in);
if (openFile.fail()) return;
record rec;
std::vector<record> songs;
while ( getline(openFile, rec.artist, ':') &&
getline(openFile, rec.title, ':') &&
getline(openFile, rec.year, ':') &&
getline(openFile, rec.category) )
songs.push_back(rec);
std::string title_remove = "title";
for (size_t i = 0; i < songs.size(); i++)
{
if (songs[i].title == title_remove)
{
songs.erase(songs.begin() + i);
break;
}
}
openFile.close();
openFile.open(filename, std::ios::out);
for (size_t i = 0; i < songs.size(); i++)
openFile << songs[i].artist << ":"
<< songs[i].title << ":"
<< songs[i].year << ":"
<< songs[i].category << "\n";
참고이 예제는 사용하는 fstream
사용하는 대신 ofstream
. 읽기를 위해 파일을 연 다음 파일을 닫고 ios::out
쓰기 플래그를 사용 하여 다시 열어야하기 때문 입니다.
void deletedvd()
{
string oldfile;
ifstream openFile;
string line, dvdnme;
ofstream createFile;
string oartist[NUM], otitle[NUM], oyear[NUM], ocategory[NUM];
string nartist[NUM], ntitle[NUM], nyear[NUM], ncategory[NUM];
//**********
//initialize these values before using them
//**********
int counter = 0;
int j = 0;
bool error = false;
bool searc = false;
//User enters filename, add .txt to make sure its saved as filename.txt
do {
cout << "Please enter the name of the file you would like to delete from: ";
cin >> oldfile;
oldfile += ".txt";
openFile.open(oldfile);
if (openFile.fail()) {
cerr << "Check spelling of file name.\n";
error = true;
}
} while (error == true);
cout << "Enter the name of the dvd you would like to delete: ";
cin >> dvdnme;
while (getline(openFile, oartist[counter], ':') && getline(openFile, otitle[counter], ':') &&
getline(openFile, oyear[counter], ':') && getline(openFile, ocategory[counter])) {
counter++;
}
//Loop for finding the deleted word, and overwriting it by shifting every to right of it left by 1 element
for (int i = 0; i < counter; i++)
{
if (dvdnme == otitle[i])
{
//**** skip this item
searc = true;
continue;
}
ntitle[j] = otitle[i];
nartist[j] = oartist[i];
nyear[j] = oyear[i];
ncategory[j] = ocategory[i];
j++;
}
if (searc)
{
//**********
//close this file handle before opening a new handle
//**********
openFile.close();
createFile.open(oldfile);//**** doesn't need the .c_str() with other std function
for (int i = 0; i < counter - 1; i++) {
createFile << ntitle[i] << ":" << nartist[i] << ":" << nyear[i]
<< ":" << ncategory[i] << endl;
}
createFile.close();
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다