C++ cli 从文件C++删除行(文本框)
我在文本框中输入文本,单击按钮时,如何从文件中删除在文本框中输入的行 我的删除方法代码:C++ cli 从文件C++删除行(文本框),c++-cli,C++ Cli,我在文本框中输入文本,单击按钮时,如何从文件中删除在文本框中输入的行 我的删除方法代码: public: System::Void deleteOneRejuser() { string fileName = "mainBase/main.txt"; fstream file; file.open(fileName, ios::in); char buf[255]; string text;
public: System::Void deleteOneRejuser()
{
string fileName = "mainBase/main.txt";
fstream file;
file.open(fileName, ios::in);
char buf[255];
string text;
//read all lines in file and write in 'buf'
while(file.getline(buf,255,'\n'));
//converting
text = (const char*) buf;
//convert textBox text in string
System::String^ myString = textBox2->Text;
string str = msclr::interop::marshal_as< string >( myString);
int pos = text.find(str);
if ( pos == (int) string::npos )
this->label2->Text = "Bad line, not found";
text.erase( pos, str.size() );
file.close();
file.open(fileName, ios::out);
file << text;
file.close();
}
VS 2010
Windows窗体您的阅读循环将读取所有行,但将丢弃除最后一行以外的所有行:
//read all lines in file and write in 'buf'
while(file.getline(buf,255,'\n'));
这是因为getline调用只是覆盖buf中的内容,而不是追加
而是做一些类似的事情
//read all lines in file and append to 'text'
while(file.getline(buf,255,'\n'))
text += buf;
你给我们展示的函数会发生什么?它有用吗?这不管用吗?编译器或运行时是否有任何错误?如果您在调试器中逐行遍历代码,它是否会像您预期的那样运行?编译器不会努力,不会出错,但不会工作。创建了一个新的控制台项目,在该项目中,您的算法可以工作,但在我看来,仍然不会查找错误,谢谢您的帮助