【发布时间】:2016-08-20 20:56:44
【问题描述】:
目前有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
我需要程序删除该行的所有信息作为一条记录。
【问题讨论】:
-
拿出一张白纸和一支笔。用简单的英语写下做你想做的事情的合乎逻辑的逐步过程。完成后,只需将编写的过程直接翻译成 C++。如果你不能写下实现这一点的逻辑过程,那么你的问题与 C++ 无关。如果可以,但您在将其中的某些部分转换为代码时遇到问题,请用 specific 问题展示您目前已实现的内容。否则,stackoverflow.com 不是代码编写服务。
-
我不是要求任何人编写它,我只是要求有关如何完成它的建议,我尝试使用 getline 创建一个新文件并复制旧文件的内容并排除该行已删除,但无法获得所有要删除的信息。我现在将重写此代码并编辑帖子,但我想可能会有更简单的东西。