基于一个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();
}
}
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何使用 c++ 实现并发文件/文本编辑?
- 我需要转换一些代码,以便它适用于输入和输出文件文本
- 通过套接字发送二进制文件.文本文件有效,其他文件无效
- 将大型数据文件拆分为多个小文件(文本格式)
- 如何读取输入文件(文本文件)并将输入验证为有效整数
- 从文本文件/文本流中读取Q字符串错误
- 从文本文件(文本解析器)C++中读取行和列的逻辑
- 如何将*.css文件(文本文件)的内容与附加信息存储在新文件中?
- 关于内存映射文件和使用大文件文本编辑器
- 如何在读取文件文本时跳过"n"
- 使用libzip从.zip获取文件(文本除外)