2014-03-13 130 views
0

我有这个类:创建C++ CLR托管DLL

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 Database::ifAccountExists() { ifstream File("Database.txt", ios_base::in);

File >>tempUser; //this line gets error I think there is problem with String^

//return true or false }

我该如何解决这个问题?

+1

这可不行,本地iostream类不知道有关托管类型如System :: String的bean。改为使用StreamReader :: ReadLine()或File :: ReadAllLines()。 –

+0

@HansPassant谢谢 –

回答

2

正如汉斯指出的那样,你将需要提供帮助的管理方,或与托管类型写入整个代码,如下所示:

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; 
} 
+0

我有另外一个问题,我正在使用这段代码: tempUser = MyInFile-> ReadLine() - > Split('|'); 我收到错误:无法从'cli :: array ^'转换为'System :: String ^' –

+0

它只会在我添加Split('|')时发生。它有点转换为另一种类型,我该如何解决这个问题? –

+0

Split函数将返回一个字符串数组。我会修改我的回应,希望这会有所帮助。 – Jeff