.net 创建C++;CLR管理的DLL
我有这门课:.net 创建C++;CLR管理的DLL,.net,build,c++-cli,.net,Build,C++ Cli,我有这门课: public ref class Database { private: String ^username, ^password, ^name, ^telNum, ^celNum, ^address, ^location; String ^tempUser; public: Database(String^, String^, String^, String^, String^, String^, String
public ref class Database
{
private:
String ^username, ^password, ^name, ^telNum, ^celNum, ^address, ^location;
String ^tempUser;
public:
Database(String^, String^, String^, String^, String^, String^, String^);
bool ifFileExists();
bool ifAccountExists();
void CreateAccount();
};
我定义了这一点:
bool数据库::ifAccountExists(){ifstream文件(“Database.txt”,
ios_base::in)
File>>tempUser;//此行出错,我想这是有问题的
字符串^
//返回true或false}
如何解决这个问题?正如Hans指出的,您需要向托管端提供帮助,或者使用托管类型编写整个代码,如下所示:
bool Database::IfAccountExists()
{
if (System::IO::File::Exists(L"Database.txt"))
{
array<System::String ^> ^lines = System::IO::File::ReadAllLines(L"Database.txt");
for each(System::String ^line in lines)
{
array<System::String ^> ^tokens = line->Split('|');
for each (System::String ^token in tokens)
{
// if found
// return true;
}
}
}
return false;
}
bool数据库::IfAccountExists()
{
如果(系统::IO::文件::存在(L“Database.txt”))
{
数组^lines=System::IO::File::ReadAllLines(L“Database.txt”);
对于每个(系统::字符串^line in line)
{
数组^tokens=line->Split(“|”);
对于每个(系统::字符串^tokens中的token)
{
//如果找到
//返回true;
}
}
}
返回false;
}
这无法工作,本机iostream类不知道有关托管类型(如System::String)的bean。改用StreamReader::ReadLine()或File::ReadAllLines()。我还有一个问题,我使用的代码是:tempUser=myinfle->ReadLine()->Split(“|”);我收到错误:无法从“cli::array^”转换为“System::String^”。只有在添加拆分(“|”)时才会发生此错误。它正在转换为另一种类型,如何解决此问题?Split函数将返回一个字符串数组。我将修改我的回答,希望能有所帮助。