小编ida*_*hmu的帖子

Python - 是否建议始终使用'b'模式打开文件?

所以我有这个简单的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)

解决二进制文件的问题(并且似乎在文本文件中保持有效)

题:

  1. 是否安全并建议始终使用rb模式读取文件?
  2. 如果没有,那么它有害的情况是什么?
  3. 如果没有,如果您不知道您正在使用哪种类型的文件,您如何知道使用哪种模式?

更新

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)

file-io binaryfiles python-2.7

5
推荐指数
1
解决办法
2003
查看次数

C++ - 以管理员身份运行进程时的GetUserName()

我有一个简单的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函数返回该线程模拟的客户端的用户名.

有没有办法获得真正登录的用户(非管理员用户),即使进程以管理员身份运行?

c++ windows-xp username windows-vista windows-7

4
推荐指数
1
解决办法
2049
查看次数

C++:检查计算机是否已锁定

我正在试图弄清楚计算机是否已锁定.

我看过LockWorkStation的功能,但我希望找到的功能是IsWorkStationLocked.


我需要支持所有windows版本> = XP

c++ windows-xp windows-7

4
推荐指数
4
解决办法
2624
查看次数

如何在特定存储库中列出包含给定提交的分支?

所以我读过Git:如何找出哪个分支标签?和许多其他帖子,我已经知道如何使用: git branch --contains tag

但是,我仍然需要知道如何在特定存储库中列出包含给定提交(或标记)的分支.

背景

archive使用以下方法从远程存储库中提取了一个标记列表:

git ls-remote --tags archive
Run Code Online (Sandbox Code Playgroud)

现在,对于每个提取的标签,我希望得到包含分支的列表.使用命令: git branch --contains tag 没有帮助,因为找不到标记,因为它们在origin存储库中不存在.

git git-tag git-branch

4
推荐指数
1
解决办法
1101
查看次数

VS2015中的wpath在哪里

刚刚升级到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;

发生了什么?

c++ visual-c++ c++14 visual-studio-2015 c++17

4
推荐指数
1
解决办法
1186
查看次数

'_WIN32_WINNT' / 'WINVER' :宏重新定义

我有一个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)

c++ windows windows-xp visual-studio-2015

4
推荐指数
1
解决办法
5823
查看次数

c ++ - 警告:右移计数> = 32位机器上的类型宽度

我有以下功能:

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)

c++ bit-shift 32bit-64bit unsigned-integer

3
推荐指数
2
解决办法
5465
查看次数

如何实现Casts实用程序命名空间

假设我生成一个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)

c++ casting lexical-cast c++11

3
推荐指数
1
解决办法
149
查看次数

Python os.path.commonprefix - 是否有面向路径的功能?

所以我有这个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)

问题 1

基于os.path.commonprefix文档:
返回最长的路径前缀(逐个字符)

是否有类似的函数:
返回最长路径前缀(由 dir 取 dir

问题2

如果commonprefix实现os.path为什么不是面向路径的,这意味着返回我想要的结果而不是真正的结果?

笔记:

我可以自己轻松地实现这一点,但如果它已经实现了为什么不使用它呢?

python path

3
推荐指数
1
解决办法
689
查看次数

我是否在滥用 std::optional

如何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

  1. 我知道使用std::optional会带来性能价格,为了争论,让我们假设我愿意支付它。
  2. 我也明白编码之类的东西: std::optional<int> iVal = 9;毫无意义。

我是不是太着迷了std::optional

我觉得std::optional很自然,它让我得出一个结论,默认情况下,我的许多本机类型bool, int, string, 应该声明为std::optional.

因此,而不是遵循以下前提:

std::optional 在绝对需要时使用

我倾向于:

std::optional始终使用,除非您确定现在或将来不需要它。

问题 1

以下规则是否有效:

使用std::optional时:

  1. 一个值是在运行时计算的
  2. 计算可能失败 …

c++ c++17 stdoptional

3
推荐指数
2
解决办法
1722
查看次数