- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试将一些数据附加到文件中,但在某些情况下想要从末尾向后跳一点以覆盖文件的尾端。但是,seekp( pos )
和 seekp( offset, relative )
对我都没有任何影响(除了在使用负偏移量时提示)。我使用不当还是它们损坏了?
下面是一个小例子。编译器:gcc 版本 4.4.4 (Debian 4.4.4-6)
#include <fstream>
#include <sstream>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
using namespace boost::posix_time;
int main(int nargs, char** pargs){
if( nargs < 2 || nargs > 3 ){
cerr<<"Usage: "<<pargs[0]<<" file [pos]"<<endl;
return 1;
}
const char* fname = pargs[1];
ofstream f( fname, ios::binary|ios::out|ios::ate );
if( !f.good() ){
cerr<<"Unable to open file!"<<endl;
return 1;
}
if( nargs == 3 ){
istringstream offss(pargs[2]);
streamoff off;
offss >> off;
cout <<"Seeking to: "<<off<<endl;
f.seekp( off, ios_base::end ); // using beg or cur instead doesn't help, nor does: seekp( off )
if( f.fail() ){
cerr<<"Unable to seek!"<<endl;
f.clear(); // clear error flags
}
}
f<<"[appending some data at "<<second_clock::local_time()<<"]\n";
return 0;
}
现在,如果我使用 0 偏移量进行查找,它应该将输出位置放在文件末尾并且写入应该追加,对吗?好吧,它对我没有影响(osf 之前不是空的):
> ./ostream_app_pos osf 0
Seeking to: 0
> cat osf
[appending some data at 2010-Jul-21 11:16:16]
通常的追加方式是使用ios::app
。在这种情况下,追加有效,但尝试使用 neg/pos 偏移量查找无效,因为(来自 gcc doc):
ios::app Seek to end of file before each write.
我还尝试既不使用 ios::ate
也不使用 ios::app
(大概是截断模式),达到与 ios::ate< 相同的效果
.
很抱歉,如果这读起来更像是一个错误报告,但我想检查一下我在使用 seekp
时是否有什么错误,并了解它是否是特定于编译器的。
最佳答案
您需要打开具有输入和输出属性的文件。
以下代码没有通常的错误处理,只是为了说明一种技术。
#include <iostream>
#include <fstream>
int main()
{
const char *szFname = "c:\\tmp\\tmp.txt";
std::fstream fs(szFname,
std::fstream::binary |
std::fstream::in |
std::fstream::out);
fs.seekp(13, std::fstream::beg);
fs << "123456789";
return 0;
}
C:\Dvl\Tmp>type c:\tmp\tmp.txt
abdcefghijklmnopqrstuvwxyz
C:\Dvl\Tmp>Test.exe
C:\Dvl\Tmp>type c:\tmp\tmp.txt
abdcefghijklm123456789wxyz
C:\Dvl\Tmp>
关于c++ - 如何恢复将数据附加到特定位置的文件? (std::ostream, streampos, tellp/seekp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3297831/
我认为stringstream的长度是由 block 计算的。这意味着它有多少 block ,它有多长。 block 被'\t''\s分割' '\n'. 例如,stringstream = '23\t
在下面的片段中,我计算了一个文件的位置,以便写入。 然后我尝试打印那个位置。它返回了一个不正确的值; printf("nXBlocks: %i nYBlocks: %i posX: %i, posY:
boost 文件锁定(可共享和作用域 file_locks)的一般策略,以及我认为一般的文件锁定,是这样的: 打开 锁 对文件内容进行操作 解锁 关闭文件 但是,我将打开文件进行追加,并想调用 tel
我正在试验 C++ 中的文件指针。在下面的代码中,获得的结果是 0、30、10 和 12。这意味着如果我们执行 seekp(),tellp() 不会在追加模式下给出正确的结果。我期待 tellp(
我正在开发一个记录器,它有一条继承 std::stringstream 的消息. 当我想打印出消息时,我调用了一个接受消息的函数 const : void logger::log_message(me
我想在一个紧密的循环中每秒调用它数千次。通话费贵吗?我正在使用 Windows Visual C++。 最佳答案 这是一个老问题,但我还是会回答,以防有其他人在寻找答案。 所以我们有一个日志库,它使用
与 fstream file("test.txt",ios_base::in | ios_base::out | ios_base::binary); tellp 和 tellg 对于 file 是否
我正在尝试使用 filtering_streams 将某些对象的序列化压缩到 array_sink 或类似设备中,然后我可以确定压缩输出的长度并将其复制到另一个流,比如文件。但是,在 filterin
每当我想将位置设置为 X tellp return -1 时,fstream 中的 seekp() 有一个小问题。 代码: fstream file("Riot.exe", ios::in | ios
我正在编写一个在文件中维护 linked_list 的程序。因此,我通过使用 tellp()/tellg() 遍历文件并将其添加到特定的长整数(可以看作是偏移量)以到达新位置。 一个简单的例子是
我对在空 ostringstream 上调用 tellp 的标准行为有疑问。我有一个函数 foo,它首先调用 tellp: void foo(std::ostream& os) { std::
#include #include int main() { const char* fileName = "out1"; std::ofstream fs1(fileName);
是否有与 C++ ostream::tellp 的 StreamWriter 等效的 C#?我正在将一些旧的 C++ 代码移植到 C#,但客户仍然想继续使用软盘(阅读:旧设备),所以我需要找到一种方法
我想为 C++ 文本输出流提供类似选项卡的功能。该功能应该允许我说“注意这个位置”,然后允许多次插入操作,最后允许我说“添加足够的填充字符,以便结束 N 个字符超过最初注意到的位置” . 标准的 io
为什么 tellg() 和 tellp() 总是在一起?我们如何将它们彼此分开? 最佳答案 他们没有。 一个表示“获取”游标在输入流中的位置;另一个指示“放置”光标在输出流中的位置。 它们相似但没有内
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在尝试将一些数据附加到文件中,但在某些情况下想要从末尾向后跳一点以覆盖文件的尾端。但是,seekp( pos ) 和 seekp( offset, relative ) 对我都没有任何影响(除了在
通过前面章节的学习我们知道,无论是使用 cout 输出普通数据,用 cout.put() 输出指定字符,还是用 cout.write() 输出指定字符串,数据都会先放到输出流缓冲区,待缓冲区刷新,数据
对于数据记录应用程序,我必须将一些(以前)未知大小的 block 写入文件。为了方便阅读,我想添加有关 block 大小的信息。与 RAM 的大小相比,数据可能很大,因此在 RAM 中使用缓冲区的解决
我在 Windows 10 和 Linux 中使用 Qt 5.7 执行相同的代码,但它只适用于 Linux。我正在尝试确定我刚刚创建的 Ostream IOstream 的大小,因此它应该是空的。在
我是一名优秀的程序员,十分优秀!