C ++查找ofstream的大小 [英] C++ find size of ofstream

查看:256
本文介绍了C ++查找ofstream的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些目前可以执行以下操作的代码:

I have code that currently does something like the following:

ofstream fout;
fout.open("file.txt");
fout<<"blah blah "<<100<<","<<3.14;
//get ofstream length here
fout<<"write more stuff"<<endl;

是否有一种简便的方法来找出在我上面指定的阶段写入的那一行的长度? (在现实生活中,int 100和float 3.14不是恒定的,可以更改).有什么好方法可以做我想做的事吗?

Is there a convenient way to find out the length of that line that is written at the stage I specified above? (in real life, the int 100 and float 3.14 are not constant and can change). Is there a good way to do what I want?

从长度上讲,我的意思是可以使用fseek进行操作的东西,例如

by length, I mean something that can be used using fseek, e.g.

fseek(pFile, -linelength, SEEK_END);

推荐答案

您要tellp.这可用于输出流(例如ostream,ofstream,stringstream).

You want tellp. This is available for output streams (e.g., ostream, ofstream, stringstream).

有一个匹配的tellg可用于输入流(例如istream,ifstream,stringstream).请注意,stringstream同时支持输入和输出,因此它同时具有tellptellg.

There's a matching tellg that's available for input streams (e.g., istream, ifstream, stringstream). Note that stringstream supports both input and output, so it has both tellp and tellg.

关于保持两个直线,p表示putg表示get,因此,如果要获取位置"(即读取位置),请使用tellg.如果要put(写)位置,请使用tellp.

As to keeping the two straight, the p means put and the g means get, so if you want the "get position" (i.e., the read position) you use tellg. If you want the put (write) position, you use tellp.

请注意,fstream同时支持输入和输出,因此它同时包含tellgtellp,但是您只能在任何给定时间调用其中之一.如果最近的操作是写操作,则可以调用tellp.如果最近的操作是读取,则可以调用tellg.否则,您将无法获得有意义的结果.

Note that fstream supports both input and output, so it includes both tellg and tellp, but you can only call one of them at any given time. If the most recent operation was a write, then you can call tellp. If the most recent operation was a read, you can call tellg. Otherwise, you don't get meaningful results.

这篇关于C ++查找ofstream的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆