基于一个 1 变量删除文本文件中的特定行
Deleting specific lines in a text file based one 1 variable
目前得到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:Ruby:xxxx:xxxx
我需要程序将行中的所有信息作为一条记录删除。
您想使用 std::vector<std::string>
而不是声明字符串数组,例如 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();
}
}
目前得到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:Ruby:xxxx:xxxx
我需要程序将行中的所有信息作为一条记录删除。
您想使用 std::vector<std::string>
而不是声明字符串数组,例如 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();
}
}