是否有更好的方法在c / c ++中创建环境变量的路径?
<script>
function readrecord() {
document.main.action = "Login.php";
document.getElementById("op").value = "check";
document.main.submit();
}
function Userdontexist() {
alert("Username or Password is wrong");
}
function submitForm() {
Traveler_Username = document.getElementById('Traveler_Username').value;
Password = document.getElementById('Password').value;
if (Traveler_Username == "" || Password == "") {
alert("Need to fill al the fields");
return;
} else {
readrecord();
}}
</script>
<?php
include 'DB_connection.php';
if (isset($_REQUEST["op"])) $op = $_REQUEST["op"]; else $op = "";
if (isset($_REQUEST["Traveler_Username"])) $Traveler_Username = $_REQUEST["Traveler_Username"];
if (isset($_REQUEST["Password"])) $Password = $_REQUEST["Password"];
if ($op=="check") {
$query = "select * from users where Traveler_Username =".$Traveler_Username;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
if (mysql_num_rows($result) == 0 ){
alert("User do not exist");
$Traveler_Username = "";
$Password = "";
header("Location: reg.php");
exit;
} else {
$query = "select Password from users where Traveler_Username =".$Traveler_Username;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);
$db_pass = $row["Password"];
if ($Password == $db_pass){
$Traveler_Username = $row['Traveler_Username'] ;
$Password = $row['Password'] ;
} else {
echo '<script> Userdontexist(); </script>';
}
} // end else
} // end if
?>
<tr>
<td style="width: 95px;">Username</td>
<td dir="rtl" style=" text-align: center; width: 195px;">
<form method="post" target="_blank">
<input id="Traveler_Username" type="text" dir="rtl" size="10" />
</form>
</td>
<td> </td>
</tr>
<tr>
<td></td>
<td class="he_shadow" style="text-align: center; width: 70px;"> </td>
<td style="width: 95px;">Password</td>
<td dir="rtl" style=" text-align: center; width: 195px;">
<form method="post" target="_blank">
<input id="Password" name="Password" type="password" dir="rtl" size="10" />
</form>
</td>
<td></td>
</tr>
<tr>
<td rowspan="2"> </td>
<td style="text-align: center; width: 70px;" rowspan="2">
</td>
<td style="width: 95px" rowspan="2">
<form method="post" target="_blank">
<input name="Submit1" type="submit" value="login" onclick="readrecord()" style="width: 43px" />
</form></td>
<td style="text-align: center; border-bottom-color: #FF9900; width: 195px; height: 30px;">
<input name="Reset1" type="reset" value="clear data" /></td>
<td rowspan="2"> </td>
</tr>
<tr>
<td style="text-align: center; border-bottom-color: #FF9900; width: 195px;">
</td>
</tr>
<tr>
<td> </td>
<td style="text-align: center; width: 70px;">
</td>
<td style="width: 95px">
</td>
<td style="text-align: center; border-bottom-color: #FF9900; width: 195px;">
</td>
<td> </td>
</tr>
</div>
答案 0 :(得分:2)
我建议让用户个人资料目录像这样。
BOOL GetCurrentUserDir(LPTSTR lpszBuf, LPDWORD lpdwBuflen)
{
HANDLE hProcessToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hProcessToken))
return FALSE;
if (!GetUserProfileDirectory(hProcessToken, lpszBuf, lpdwBuflen)) {
CloseHandle(hProcessToken);
return FALSE;
}
CloseHandle(hProcessToken);
return TRUE;
}
我像这样使用这个功能:
WCHAR szLocalPath[MAX_PATH];
if (!GetCurrentUserDir(szLocalPath, &cchPath))
{
//Handle the error
}
然后您可以追加路径的其余部分。
这封装了特定的环境变量并使用了Win32 API。随着时间的推移,这更有可能保持便携性。
答案 1 :(得分:1)
为什么不使用std :: string?
std::string s1 = std::getenv("SystemDrive");
s1 += "\\\\Users\\\\" + std::getenv("USERNAME") + "\\\\Documents";
答案 2 :(得分:1)
连接多个字符串的一个好方法是使用snprintf
:
char buf[0x10000];
int n = std::snprintf(buf, sizeof buf, "%s\\\\Users\\\\%s\\\\Documents", S1, S2);
if(static_cast<size_t>(n) >= sizeof buf)
// Buffer is too small (if n > 0) or error
snprintf
不会溢出缓冲区并始终为零终止的事实会使不安全的函数strcpy
,strncpy
,strcat
变得不必要。
答案 3 :(得分:0)
不确定完全了解您的需求。这是从环境变量创建路径吗?或者获取系统目录的正确路径?在后一种情况下,您最好使用Win32 API,例如:SHGetFolderPath
或SHGetKnownFolderPath
。
#include <Windows.h>
#include <ShellAPI.h>
#include <KnownFolders.h>
#include <ShlObj.h>
int main()
{
CoInitialize(NULL);
TCHAR* path = 0;
SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_SIMPLE_IDLIST, NULL, &path);
// use collected 'path' here
CoTaskMemFree(path); // free the TCHAR object allocated by SHGetKnownFolderPath
return 0;
}
以上代码检索当前用户&#34;我的文档&#34;文件夹路径。
答案 4 :(得分:-1)
std::getenv
已被弃用。
我会用喜欢的东西:
char* wSystemDrive = nullptr;
size_t wSize = 0;
bool wFree = true;
if(_dupenv_s(&wSystemDrive,&wSize,"SystemDrive") != 0 || wSystemDrive == nullptr)
{
wFree = false;
wSystemDrive = "C:\\"; //or anything default you want to use
}
std::string wSysteDriveString(wSystemDrive);
if(wFree)
{
free(wSystemDrive);
}
char* wUSERNAME = nullptr;
wSize = 0;
wFree = true;
if(_dupenv_s(&wEnv,&wSize,"wUSERNAME") != 0 || wUSERNAME == nullptr)
{
wFree = false;
wUSERNAME = "User";
}
std::string wUSERNAMEString(wUSERNAME);
if(wFree)
{
free(wUSERNAME);
}
std::string wPath = wSystemDriveString + "\\Users\\" + wUSERNAMEString + "\\Documents"
请注意,您不需要4 \
(\\\\
),只需要两个\\
。