//
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
void addMember();
void getChoice();
double nextID();
void removeMember();
void displayMember();
using namespace std;
void removeMember()
{
string membername;
double line, deleteline;
system("cls");
cout<<"--------------------REMOVE MEMBER MENU--------------------\n";
displayMember();
ifstream memberList;
memberList.open("member.txt");
ofstream temp;
temp.open("temp.txt");
cout<<"Enter the member to remove: ";
cin>>deleteline;
//Statement to deleteline.
temp.close();
memberList.close();
}
从removeMember()函数中,我应该使用什么语句? 我应该使用while循环或数组(逐个搜索信息)? 对于此功能,我需要执行的是当用户键入成员名称(删除行)时,该功能将跟踪名称并删除所需的行。
member.txt
0001 name1
0002 name2
0003 name3
temp.txt是一个空文件
对于搞砸的语法,我很抱歉,我是C ++编程的新手。
答案 0 :(得分:0)
因此,您可以更改memberList
的分隔符,而不会过于复杂,这应该只允许您使用istream_iterator
。
void removeMember()
{
system("cls");
cout<<"--------------------REMOVE MEMBER MENU--------------------\n";
displayMember();
const ctype<char>::mask foo[ctype<char>::table_size] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ctype_base::space };
int deleteline = -1; // Changed this to an int and initialized in case cin fails
ifstream memberList("member.txt"); // Used ifstream constructor
ofstream temp("temp.txt"); // Used ofstream constructor
cout << "Enter the member to remove: ";
cin >> deleteline;
memberList.imbue(local(foo.getloc(), new ctype<char>(foo)));
copy_if(istream_iterator<string>(memberList), istream_iterator<string>(), ostream_iterator<string>(temp, "\n"), [&](const string& i){return --deleteline != 0;});
} // Removed the closes as they will happen automatically when the fstreams go out of scope
请注意,对于此黑客攻击,您应该#include <locale>
,#include <iterator>
和#include <algorithm>
。而且这个hack假定'\n'
是foo
形成的第10个字符。