我想要:
编辑:使用truncate我的意思是写入一个位置并丢弃文件的剩余部分(如果存在)
所有这些原子(通过单个open()调用或模拟单个open()调用)
似乎没有单一的开放模态适用:
我试过的一些组合(rw,rw +,r + w等)似乎也不起作用.可能吗?
来自Ruby的一些文档(也适用于python):
r
Read-only mode. The file pointer is placed at the beginning of the file.
This is the default mode.
r+
Read-write mode. The file pointer will be at the beginning of the file.
w
Write-only mode. Overwrites the file if the file exists. If the file
does not exist, creates a new …Run Code Online (Sandbox Code Playgroud) 我读过那个<fstream>早期的<exception>.忽略了例外fstream情况不是很有用的事实,我有以下问题:
可以使用该exceptions()方法在文件流上启用异常.
ifstream stream;
stream.exceptions(ifstream::failbit | ifstream::badbit);
stream.open(filename.c_str(), ios::binary);
Run Code Online (Sandbox Code Playgroud)
任何尝试打开不存在的文件,没有正确权限的文件或任何其他I/O问题都将导致异常.使用自信的编程风格非常好.该文件应该在那里并且可读.如果条件不满足,我们会得到例外.如果我不确定文件是否可以安全打开,我可以使用其他功能来测试它.
但现在假设我尝试读取缓冲区,如下所示:
char buffer[10];
stream.read(buffer, sizeof(buffer));
Run Code Online (Sandbox Code Playgroud)
如果流在填充缓冲区之前检测到文件结束,则流决定设置failbit,如果启用了异常,则会触发异常.为什么?这有什么意义?我本可以eof()在阅读后验证只是测试:
char buffer[10];
stream.read(buffer, sizeof(buffer));
if (stream.eof()) // or stream.gcount() != sizeof(buffer)
// handle eof myself
Run Code Online (Sandbox Code Playgroud)
这种设计选择使我无法在流上使用标准异常,并迫使我在权限或I/O错误上创建自己的异常处理.或者我错过了什么?有什么出路吗?例如,我可以轻松测试是否可以sizeof(buffer)在流之前读取字节数吗?
我正在使用WixUI_Advanced序列来允许用户选择每台机器或每用户安装并更改目标文件夹.我的WiX项目旨在生产x86和x64 MSI(我正在使用WiX提示和技巧建议).我还将应用程序安装文件夹保留在注册表中以进行主要升级(我使用APPLICATIONFOLDER属性和目录ID - 而不是INSTALLLOCATION - 根据WixUI_Advanced要求).
WixUI_Advanced序列中存在一个错误,导致目标文件夹对话框在64位计算机上运行时在C:\ Program Files(x86)下显示app文件夹而不是C:\ Program Files,即使代码正确设置了app文件夹到ProgramFiles64Folder属性.错误跟踪器注释建议使用SetDirectory元素来设置APPLICATIONFOLDER的值,但没有解释如何执行此操作的示例.当我尝试时,它确实有点不同(我还发现一些帖子建议使用自定义动作设置APPLICATIONFOLDER,但没有一个对我有效).有谁知道如何使WixUI_Advanced序列在64位系统上显示正确的'Program Files'文件夹(并在主要升级期间显示最初选择的文件夹)?
如果它有帮助,我将提供示例WXS片段,但它们几乎遵循StackOverflow的WiX提示和技巧帖子的建议.此外,我的64位MSI包确实是一个64位的包(我的包和组件标记为'x64';它不能在32位平台上运行.)我正在使用WiX 3.6和Visual Studio 2010 .
代码示例:
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product
Id="81955f17-31f3-4e51-8294-372f96141c00"
Name="WiX64BitDemo"
Language="1033"
Version="1.0.0.0"
Manufacturer="Test"
UpgradeCode="5bed9777-bea6-4dc3-91d7-5dd93819563a">
<Package
InstallerVersion="300"
Compressed="yes"
InstallScope="perMachine"
Platform="x64" />
<MajorUpgrade
AllowSameVersionUpgrades="no"
DowngradeErrorMessage="Can't downgrade."
Schedule="afterInstallInitialize" />
<Media
Id="1"
Cabinet="media1.cab"
EmbedCab="yes" />
<Property …Run Code Online (Sandbox Code Playgroud) 我正在尝试最近的std::chronoapi,我发现在64位Linux体系结构和gcc编译器上time_point,duration类和类无法以最大分辨率(纳秒)处理操作系统的最大时间范围.实际上,似乎这些类的存储是一个64位的整数类型,相比于timespec和timeval其内部使用两个64位整数,一个用于秒,一个用于纳秒:
#include <iostream>
#include <chrono>
#include <typeinfo>
#include <time.h>
using namespace std;
using namespace std::chrono;
int main()
{
cout << sizeof(time_point<nanoseconds>) << endl; // 8
cout << sizeof(time_point<nanoseconds>::duration) << endl; // 8
cout << sizeof(time_point<nanoseconds>::duration::rep) << endl; // 8
cout << typeid(time_point<nanoseconds>::duration::rep).name() << endl; // l
cout << sizeof(struct timespec) << endl; // 16
cout << sizeof(struct timeval) << endl; // 16
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在64位Windows(MSVC2017)上,情况非常相似:存储类型也是64位整数.在处理稳定(也称为单调)时钟时这不是问题,但存储限制使得不同的API实现不适合存储更大的日期和更宽的时间跨度,从而为类似Y2K的错误创造了基础.这个问题得到了承认吗?是否有更好的实施或API改进的计划?
我正在尝试在我的C#应用程序中实现块文件锁定.如果内置FileStream.Lock方法无法获取锁定,则抛出异常.
底层LockFile方法返回状态代码但是我不想使用自旋锁来等待文件被解锁.
有没有人在C#中有任何代码片段,显示如何使用句柄正确构造OVERLAPPED结构wait并将其传递给LockFileEx等待操作完成?我试图避免使用Overlapped.Pack方法,因为它们不安全,但主要是因为它们需要的IOCompletionCallback不是我想要实现的.
我有声明,但结构的构造和使用OverLapped似乎有点复杂.
注意:我知道我需要手动固定重叠结构,直到等待完成.我目前的代码如下:
ManualResetEvent evt = new ManualResetEvent(false);
OVERLAPPED overlapped = new OVERLAPPED();
overlapped.OffsetLow = offsetLow;
overlapped.OffsetHigh = offsetHigh;
overlapped.hEvent = evt.SafeHandle;
GCHandle h = GCHandle.Alloc(overlapped, GCHandleType.Pinned);
int hr = Win32.LockFileEX(_handle, LockFlags.Exclusive, 0, offsetLow, offsetHigh,
GCHandle.ToIntPtr(h));
if(hr == 0)
{
int error = Marshal.GetLastWin32Error();
if(error = Win32.ERROR_IO_PENDING)
{
evt.WaitOne();
}
else
{
//ohpoo
}
}
Run Code Online (Sandbox Code Playgroud)
最终按照我的意愿工作的代码是:
[StructLayout(LayoutKind.Sequential)]
public …Run Code Online (Sandbox Code Playgroud) 我找到了StackOverFlow的答案和其他资源,说你可以点击一个隐藏的ASP:按钮与jQuery by
$("#<%=HiddenButton.ClientID%>").click();
Run Code Online (Sandbox Code Playgroud)
要么
$("#<%=HiddenButton.ClientID%>").trigger("click");
Run Code Online (Sandbox Code Playgroud)
但是,这些都不适用于我,除非按钮是Visible ="true"
这是按钮:
<asp:Button ID="loadCustomerContacts" runat="server" OnClick="loadCustomerContacts_Click" visible="false" />"
Run Code Online (Sandbox Code Playgroud) 当我用鼠标控制此控件时,我想转发一条消息(例如WM_MOUSEWHEEL),而不会窃取焦点.使用IMessageFilter(要添加到应用程序消息泵)并使用P/Invoke(d)SendMessage()转发消息,可以轻松解决此问题.问题是:我可以不使用P/Invoke做同样的事情(我在StackOverflow中找到的解决方案使用P/Invoke)吗?如果没有,为什么?
下面的代码是我用P/Invoke的解决方案.我只是用它new MessageForwarder(control, 0x20A).
/// <summary>
/// This class implements a filter for the Windows.Forms message pump allowing a
/// specific message to be forwarded to the Control specified in the constructor.
/// Adding and removing of the filter is done automatically.
/// </summary>
public class MessageForwarder : IMessageFilter
{
#region Fields
private Control _Control;
private Control _PreviousParent;
private HashSet<int> _Messages;
private bool _IsMouseOverControl;
#endregion // Fields
#region Constructors
public MessageForwarder(Control control, int message)
: this(control, new int[] …Run Code Online (Sandbox Code Playgroud) 我发现自己处于这样一种情况,我希望有一个类似于unique_ptrs release()for的情况std::vector<>。例如:
std::vector<int> v(SOME_SIZE);
//.. performing operations on v
int* data = v.release(); // v.size() is now 0 and the ownership of the internal array is released
functionUsingAndInternallyDeletingRowPointer(data);
Run Code Online (Sandbox Code Playgroud)
有什么特殊原因不提供这种可能性吗?std::vector这会对内部实施施加一些限制吗?
或者有一种方法可以实现这一点,而我却尴尬地错过了?
假设我有一个WeakReference目标强引用.我希望在GC收集目标对象本身时得到通知.可能吗?
编辑:在终结器/析构函数中添加代码不是一个选项.我需要一些不依赖于类代码的东西.
.net c# garbage-collection weak-references strong-references
我正在尝试记录WebRTC传输的音频PeerConnection MediaStream。我在实现的音轨中添加了一个接收器AudioTrackSinkInterface。它实现了以下OnData方法:
void TestAudioTrackSink::OnData(const void* audio_data, int bits_per_sample, int sample_rate, size_t number_of_channels, size_t number_of_frames) {
size_t valueCount = number_of_channels * number_of_frames;
int16_t *_data = (int16_t*)audio_data;
f.write((char*)&_data, sizeof(int16_t) * valueCount);
f.flush();
}
Run Code Online (Sandbox Code Playgroud)
f是一个ofstream。每个样本的位数为16,采样率为 16000,通道为1,帧为160。
但是,当我使用AudaCity原始导入(带符号的16位PCM,小字节序,单声道,采样率16000)打开创建的文件时,我听不到有意义的音频。
如何正确写原始音频日期?
我有一个TestNet本机组件的包装器。本机组件公开了一个通过调用托管回调与托管部分进行通信的阻塞 ,以及一个用于检索对 .NET 包装器的引用并提供上下文的弱函数。它很弱,因为 .NET 包装器旨在隐藏正在向用户处理非托管资源的事实,并且故意不实现该接口:非弱它会根本阻止实例被收集,从而造成内存泄漏。发生的情况是,在发布版本中,只有垃圾收集器会在执行托管回调时收集对 .NET 包装器的引用,甚至在两者之前并且令人惊讶地解除阻塞。我自己理解这个问题,我可以通过在 P/Invoke 调用后发出 a 来解决它,但由于这方面的知识不是很广泛,似乎很多人都做错了。我有几个问题:TestNative::Foo()GCHandleGCHandleIDisposableTestNetTestNative::Foo()TestNet::Foo()GC.KeepAlive(this)
GC.KeepAlive(this)最后一条指令是对非托管资源的 P/Invoke 调用,或者仅在这种特殊情况下需要,即在从本机代码封送托管回调时切换到托管执行上下文,则托管方法中始终需要该指令?问题可能是:我应该GC.KeepAlive(this)到处放吗?这个旧的微软博客(原始链接是404,这里被缓存)似乎是这么建议的!但这将改变游戏规则,基本上这意味着大多数人从未正确执行过 P/Invoke,因为这需要在包装器中检查大多数 P/Invoke 调用。例如,是否有一条规则规定,当执行上下文非托管(本机)时,垃圾收集器(编辑:或更好的终结器)无法为属于当前线程的对象运行?GC.KeepAlive(this) 策略。一般来说,在处理终结器GC.KeepAlive(this)时似乎很少需要。注意:我对收集代表没有问题,这是一个不同的问题,我知道如何正确处理。这里的问题是,当 P/Invoke 调用尚未完成时,会收集持有非托管资源的对象。
它遵循的代码清楚地表明了问题。创建一个 C# 控制台应用程序和一个 C++ Dll1项目并在发布模式下构建它们:
程序.cs:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
class Program
{
static void …Run Code Online (Sandbox Code Playgroud) gcc支持使用## __VA_ARGS__约定对零参数计数宏.以下使用gcc编译的工作:
#include <stdio.h>
#define NARGS(...) __NARGS(0, ## __VA_ARGS__, 5,4,3,2,1,0)
#define __NARGS(_0,_1,_2,_3,_4,_5,N,...) N
int main()
{
printf("%d\n", NARGS()); // prints 0
printf("%d\n", NARGS(1)); // prints 1
printf("%d\n", NARGS(1, 2)); // prints 2
return 0;
}
Run Code Online (Sandbox Code Playgroud)
VisualC++ 2010是否有与零参数宏一起使用的等价物?接受非标准扩展或技巧.
编辑:修复了使用GCC扩展和C++编译器的示例.
我想创建一个自定义分配器basic_string,允许我获得分配的字符串内部数组的所有权。我的具体用例是 .NET 互操作场景,其中将字符串封送回托管代码的成本很高,因为它需要在特定池中分配字符串(至少在Windows 中),更重要的是堆中数组的所有权必须是转入。我能够成功地为std::vector主要编译器(MSVC、gcc、clang)编写此类自定义分配器并验证其兼容性。我现在尝试使用相同的分配器basic_string,我观察到奇怪的行为,因为所有主要的 STL 实现似乎都没有使用提供的分配器进行第一次分配,特别是前 16 个字节。它遵循我正在使用的代码:
#include <memory>
#include <stdexcept>
#include <vector>
#include <iostream>
// The requirements for the allocator where taken from Howard Hinnant tutorial:
// https://howardhinnant.github.io/allocator_boilerplate.html
template <typename T>
struct MyAllocation
{
size_t Size = 0;
std::unique_ptr<T> Ptr;
MyAllocation() { }
MyAllocation(MyAllocation && other) noexcept
: Ptr(std::move(other.Ptr)), Size(other.Size)
{
other.Size = 0;
}
};
// This allocator keep ownership of the last allocate(n)
template <typename T>
class MyAllocator …Run Code Online (Sandbox Code Playgroud) c++ ×5
c# ×4
.net ×2
allocator ×2
stl ×2
visual-c++ ×2
asp.net ×1
audio ×1
c++-chrono ×1
c++11 ×1
callback ×1
char ×1
eof ×1
file-io ×1
finalizer ×1
focus ×1
fstream ×1
gcc ×1
locking ×1
macros ×1
message-pump ×1
mousewheel ×1
pinvoke ×1
python ×1
ruby ×1
std ×1
stdstring ×1
truncate ×1
vector ×1
webforms ×1
webrtc ×1
windows ×1
wix ×1
wix3.6 ×1
year2038 ×1