所以我有这个简单的python函数:
def ReadFile(FilePath):
with open(FilePath, 'r') as f:
FileContent = f.readlines()
return FileContent
Run Code Online (Sandbox Code Playgroud)
此函数是通用的,用于打开所有类型的文件.但是,当打开的文件是二进制文件时,此功能无法按预期执行.将open()调用更改为:
with open(FilePath, 'rb') as f:
Run Code Online (Sandbox Code Playgroud)
解决二进制文件的问题(并且似乎在文本文件中保持有效)
rb模式读取文件?FilePath = r'f1.txt'
def ReadFileT(FilePath):
with open(FilePath, 'r') as f:
FileContent = f.readlines()
return FileContent
def ReadFileB(FilePath):
with open(FilePath, 'rb') as f:
FileContent = f.readlines()
return FileContent
with open("Read_r_Write_w", 'w') as f:
f.writelines(ReadFileT(FilePath))
with open("Read_r_Write_wb", 'wb') as f:
f.writelines(ReadFileT(FilePath))
with open("Read_b_Write_w", 'w') as f:
f.writelines(ReadFileB(FilePath))
with open("Read_b_Write_wb", 'wb') as …Run Code Online (Sandbox Code Playgroud) 我有一个简单的C++程序,提示用户名
#include <windows.h>
#include <Lmcons.h>
#include <winbase.h>
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
::GetUserName(username, &username_len);
MessageBox(NULL, username, NULL, 1);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
GetUserName()在管理员帐户中按预期执行,这意味着打印真实用户名.
但是,在非管理员帐户中以管理员身份运行时,我会获得管理员名称,而不是真实登录用户.
我相信这种行为是预期的,因为它记录在GetUserName()中:
如果当前线程模拟另一个客户端,则GetUserName函数返回该线程模拟的客户端的用户名.
有没有办法获得真正登录的用户(非管理员用户),即使进程以管理员身份运行?
所以我读过Git:如何找出哪个分支标签?和许多其他帖子,我已经知道如何使用:
git branch --contains tag
但是,我仍然需要知道如何在特定存储库中列出包含给定提交(或标记)的分支.
我archive使用以下方法从远程存储库中提取了一个标记列表:
git ls-remote --tags archive
Run Code Online (Sandbox Code Playgroud)
现在,对于每个提取的标签,我希望得到包含分支的列表.使用命令:
git branch --contains tag
没有帮助,因为找不到标记,因为它们在origin存储库中不存在.
刚刚升级到VS 2015(来自VS 2013).试图编译我的项目,但wpath(我使用很多!)无法识别.打开filesystem文件:
...Microsoft Visual Studio 14.0\VC\include\filesystem
比较它:
...Microsoft Visual Studio 12.0\VC\include\filesystem
并且wpath不再存在.
过去它被定义为:
typedef basic_path<wstring, wpath_traits> wpath;
发生了什么?
我有一个VS2015 C++项目。应用程序必须在 Windows 7 和 XP 上运行。所以,我想将_WIN32_WINNT&设置WINVER为_WIN32_WINNT_WINXP。
这就是stdafx.h我的项目的样子:
stdafx.h
#pragma once
#include "targetver.h"
#define _WIN32_WINNT _WIN32_WINNT_WINXP
#define WINVER _WIN32_WINNT_WINXP
#include <WinSDKVer.h>
// Windows Header Files:
#include <windows.h>
Run Code Online (Sandbox Code Playgroud)
编译时,我看到以下警告/错误:
stdafx.h(12): error C2220: warning treated as error - no 'object' file generated
1>stdafx.h(12): warning C4005: '_WIN32_WINNT': macro redefinition
1> C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\SDKDDKVer.h(197): note: see previous definition of '_WIN32_WINNT'
1>stdafx.h(13): warning C4005: 'WINVER': macro redefinition
1> C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\SDKDDKVer.h(212): note: see …Run Code Online (Sandbox Code Playgroud) 我有以下功能:
void func(unsigned long v)
{
char max_byte = 0xFF;
char buffer[8];
buffer[0] = static_cast<char>((v) & max_byte);
buffer[1] = static_cast<char>((v >> 8) & max_byte);
buffer[2] = static_cast<char>((v >> 16) & max_byte);
buffer[3] = static_cast<char>((v >> 24) & max_byte);
buffer[4] = static_cast<char>((v >> 32) & max_byte);
buffer[5] = static_cast<char>((v >> 40) & max_byte);
buffer[6] = static_cast<char>((v >> 48) & max_byte);
buffer[7] = static_cast<char>((v >> 56) & max_byte);
}
Run Code Online (Sandbox Code Playgroud)
它接受一个unsigned long参数并将其8个字节插入char缓冲区(不要试图找出原因.它是一个有意义的函数的简洁版本).
此代码在64位上编译良好,但在32位上我得到以下警告:
warning: right shift count >= width …Run Code Online (Sandbox Code Playgroud) 假设我生成一个Casts名称空间,它将包含许多强制转换函数:
namespace Casts
{
// To string
bool Cast(bool bValue, string& res);
bool Cast(int intValue, string& res);
bool Cast(float floatValue, string& res);
bool Cast(const wstring& str, string& res);
// From string
bool Cast(const string& strVal, bool& res);
bool Cast(const string& strVal, int& res);
bool Cast(const string& strVal, long& res);
bool Cast(const string& strVal, float& res);
// And lots of other casting functions of different types
}
Run Code Online (Sandbox Code Playgroud)
我真的很喜欢boost:lexical_cast方法.例如:
bool Cast(int intValue, string& res)
{
bool bRes = …Run Code Online (Sandbox Code Playgroud) 所以我有这个python代码:
print os.path.commonprefix([r'C:\root\dir',r'C:\root\dir1'])
Run Code Online (Sandbox Code Playgroud)
C:\root\dir
Run Code Online (Sandbox Code Playgroud)
C:\root
Run Code Online (Sandbox Code Playgroud)
基于os.path.commonprefix文档:
返回最长的路径前缀(逐个字符)
是否有类似的函数:
返回最长路径前缀(由 dir 取 dir)
如果commonprefix实现os.path为什么不是面向路径的,这意味着返回我想要的结果而不是真正的结果?
我可以自己轻松地实现这一点,但如果它已经实现了为什么不使用它呢?
std::optional适合我的代码?我的代码包含许多看起来大致如下的函数:
bool GetName(_Out_ string& strRes)
{
// try to get/calculate the value
// value may also be empty
// return true on success, false otherwise.
}
Run Code Online (Sandbox Code Playgroud)
既然我已经找到了std::optional,我只需编写代码:
std::optional<std::string> GetName()
Run Code Online (Sandbox Code Playgroud)
std::optional?std::optional会带来性能价格,为了争论,让我们假设我愿意支付它。std::optional<int> iVal = 9;毫无意义。std::optional?我觉得std::optional很自然,它让我得出一个结论,默认情况下,我的许多本机类型bool, int, string, 应该声明为std::optional.
因此,而不是遵循以下前提:
std::optional仅在绝对需要时使用
我倾向于:
std::optional始终使用,除非您确定现在或将来不需要它。
以下规则是否有效:
使用std::optional时:
c++ ×7
windows-xp ×3
c++17 ×2
windows-7 ×2
32bit-64bit ×1
binaryfiles ×1
bit-shift ×1
c++11 ×1
c++14 ×1
casting ×1
file-io ×1
git ×1
git-branch ×1
git-tag ×1
lexical-cast ×1
path ×1
python ×1
python-2.7 ×1
stdoptional ×1
username ×1
visual-c++ ×1
windows ×1