由买买提看人间百态

topics

全部话题 - 话题: ofstream
1 (共1页)
b***i
发帖数: 3043
1
来自主题: Programming版 - ofstream and cout question
I want to use cout or cerr to output something in debug mode, but not
release mode
one way is to
#define myout cout
and use myout always when debug and change the definiton to some null file.
Is there a way to dynamically change the myout if it's declared as
ofstream myout;
such as, myout=cout; (this wouldn't work)
thanks.
b***y
发帖数: 2799
2
☆─────────────────────────────────────☆
birdsman (birdsman) 于 (Mon Mar 24 03:30:52 2008) 提到:
VS2005下面很简单的一段代码,
ofstream os("f", ios::binary);
float iii = 7.00001;
os << iii;
os.close();
结果得到的文件f有7个字节,显然是text mode
有谁知道这是咋回事? 多谢
☆─────────────────────────────────────☆
birdsman (birdsman) 于 (Mon Mar 24 03:49:51 2008) 提到:
我觉得不是,如果bin模式存,应该只会占用4个字节

☆─────────────────────────────────────☆
birdsman (birdsman) 于 (Mon Mar 24 03:57:29 2008) 提到:
昏,当然不是。
float是4个bytes,7.00001的text表示就是7个bytes
c******g
发帖数: 63
3
来自主题: Programming版 - C++ ofstream的一个诡异问题
一个比较大的仿真程序(NS-2,很多做网络的人应该都用过)里面,想把数据输出到文
件。在C++源码某个相关类里加了个ofstream类的成员函数ofs,开始仿真的入口点初始
化(也就是以ios::trunc打开相应文件),在此后的代码中输出时,在ofs << buf外头
加个if (即 if (ofs << buf) {...})也没有问题,但是输出的文件一直是空的(用
cout则可以确定buf里是有内容的),感觉很诡异。
于是,在输出前检验了ofs.is_open()、ofs.good()和ofs.bad(),发现值为true,
false, true。试了ofs.clear(),发现没用,就改用每次输出前重新用ios::app方式打
开一次这个文件(文件名是记在另一个成员变量outFileName中的),居然就work了,
但是这样会让程序速度大大减慢。于是另加了一句判断 if (!ofs.is_open() || !ofs.
good()) 才重新打开这个文件。这样速度提升了,内容也都写进去了。。。但是这还没
完。。。
改了一些其他的仿真参数仿真,又不行了(这诡异到居然是scen... 阅读全帖
w***g
发帖数: 5958
4
来自主题: Programming版 - C++ ofstream的一个诡异问题
你试试给ofs.ofstream或者ofs.open加断点, 看是什么时候执行的. 根据你的描述有可
能是这么一种情况, 就是NS-2由于某些特殊的需要可能会在正常simulation之后又创建
了一个带有ofs的instance, 然后把你的正常输出给trunc了.
或者你每次用ofs打开文件的时候用不同的文件名看看.

ofs.
x******a
发帖数: 6336
5
来自主题: Programming版 - C++ problem
I got thousands problems on the following piece of code "dumpfile.h" when I
compile under cygwin. it is ok under visual stduio... can anyone help?
Thanks!
#include
#include
#include //ostream_iterator
#include //cerr
#include //std::copy
template
void dump_to_file(const char* filename, const std::vector& v_d){
std::ofstream ofs(filename);
if(!ofs){
std::cerr<<"Unable to open the file to write!n";
return ;... 阅读全帖
w********0
发帖数: 1211
6
非常感谢sunnyedinken, jyjyjjyy, binrose,现在编译倒是通过了,无论是加上using std::fstream, 或者整个
的using namespace std都行。但新问题又来了,运行起来打不开文件。
我照着Lippman那本书写的:
ofstream outfile;
outfile.open("test.txt");
if (!outfile) {
cerr << "error: unable to open output file: \n ";
}
outfile.close();
运行起来总是告诉我 error: unable to open output file:
也就是文件根本就没打开。
我尝试着建立一个文件放在目录里(和source,header同一个目录下) ,也没用。
ifstream,ofstream都不行。
也尝试过定义的时候直接bind: ofstream outfile("test.txt"), 还是不行。
看来我实在太弱了。
********************... 阅读全帖
s***e
发帖数: 122
7
来自主题: CS版 - 问一个c++问题
那最好还是定义一个自己的输出输入类吧。这样以后万一修改代码的时候也不容易漏掉
。刚才写了一个例子,也是做个练习,呵呵,以前也没有自己写过。你就作为一个参考
吧。
// MyIn.h
#ifndef _MY_IN_H_
#define _MY_IN_H_
#include
#include
#include
class MyIn
{
private:
std::ofstream& ofs;
public:
MyIn(std::ofstream& o);
public:
MyIn& operator >> (int& i);
MyIn& operator >> (std::string& s);
};
#endif
// MyIn.cpp
#include "MyIn.h"
MyIn::MyIn(std::ofstream& o):ofs(o) {
}
MyIn& MyIn::operator >> (int& i) {
std::cin >> i;
ofs << i << std::endl;
return *this;
}
w********0
发帖数: 1211
8
非常感谢你们几位,现在编译倒是通过了,无论是加上using std::fstream, 或者整个
的using namespace std都行。但新问题又来了,运行起来打不开文件。
我照着Lippman那本书写的:
ofstream outfile;
outfile.open("test.txt");
if (!outfile) {
cerr << "error: unable to open output file: \n ";
}
outfile.close();
运行起来总是告诉我 error: unable to open output file:
也就是文件根本就没打开。
我尝试着建立一个文件放在目录里(和source,header同一个目录下) ,也没用。
ifstream,ofstream都不行。
也尝试过定义的时候直接bind: ofstream outfile("test.txt"), 还是不行。
看来我实在太弱了。
s*****n
发帖数: 231
9
来自主题: JobHunting版 - C++ 一问?
cout is an object of class ostream that represents the standard output
stream.
and ostream is the base class of ofstream, which you use to output to a file.
So in A. void print(std::ostream &os);
if you pass a reference to ostream object in the print function, it's OK for
both type.
But in B.void print(std::ofstream is);
Not only you cannot output to the standard output, but also it's passed by
value, that means you make a copy of the ostream object you passed in, and
do output in that copy, whi... 阅读全帖
d***a
发帖数: 316
10
来自主题: CS版 - 改变 c string 的一个问题
想法是用户输入一个文件名,例如 someresults.txt
然后用ifstream读入,处理后,用ofstream保存为 someresults_data_extracted.txt
用户输入的文件后缀要去掉。
以下是产生问题的部分code,其它省略。
整个程序g++编译通过的。

#include
#include
#include
#include

cout << "Enter the file to work on: ";
string originName;
getline(cin, originName);


// code for ifstream to read a file
…..


char * tempOut = new char [originName.size()+16];
strncpy(tempOut, originName.c_str(), originName.size()-4);
ofstream fout;
fout.open( strcat(tempO... 阅读全帖
z****u
发帖数: 15
11

You are not using the right classes. In JDK1.1 and later,
use
PrintWriter dos = new PrintWriter( new
FileWriter("file.txt"));
dos.println("a string here");
why do you think it is NOT simple?
In C:
FILE dos = fopen("file.txt", "w");
fprint(dos, "a string here\n");
In C++
ofstream dos = new ofstream("file.txt");
dos << "a string here" << endl;
same logic, same approach
s*****n
发帖数: 1279
12
来自主题: Programming版 - 一个极简单的程序求教
以前用C++都是在interpreter的环境下,现在想编译,发现好多都不懂。大家帮我看看下
面这个程序:
#include
#include
#include
#include
#include
int main (int argc, char **argv)
{
int iarg = 1;
char root_file_name[256];
strcpy (single_file_name,argv[1]);
strcpy (root_file_name, argv[2]);

ofstream decayresults(root_file_name);
decayresults.close();
cout<<"That's all"<
}//end of main
然后我用下面的Makefile编译总是通不过,说是ofstream,cout, endl 都没有定义。难
道不是只要include ,
v****c
发帖数: 32
13
来自主题: Programming版 - fstream 扫盲,谢谢!
为啥这种编译过不了?
ifstream in_file;
ofstream out_file;
in_file("data.txt");
out_file("data.sort");
必须得:
ifstream in_file("data.txt");
ofstream out_file("data.sort");
i**p
发帖数: 902
14
来自主题: Programming版 - C++: Static initialization dependency
I have questions after reading "Thinking in C++" volume 1, Chapter 10 Name
Control, section Static initialization dependency. Hopefully people here can
explain them to me.
1. Why does it mention "static objects"? In fact there is no static object
in the example code but global class objects only.
2. Because of the dependency, the C++ compiler "should" guarantee the order,
right? Otherwise it is a bug.
Here is from the book.
---------------------------------------------------------
there is no gu... 阅读全帖
h*****g
发帖数: 312
15
来自主题: JobHunting版 - C++ 一问?
You have to implement a function that needs to be able to write to both
standard output (typically the console screen) and files. Which one of the
following function declarations satisfies that need?
A.
void print(std::ostream &os);
B.
void print(std::ofstream is);
C.
void print(std::cout);
D.
void print(std::istream is);
E.
void print(std::istream &is);
问下,B 为啥不行呢?A 可以输出到文件吗?网上没找到答案~~~
j*****y
发帖数: 1071
16
来自主题: JobHunting版 - 发一个刚面的startup面经
第二题我习惯用 fstream
ofstream file(inputFile);
for(int i = 0; i < n; ++i)
{
string s(array[i]);
s.append(1, '\0');
file<< s;
}
file.close();
r*********n
发帖数: 4553
17
来自主题: JobHunting版 - G家面经 已跪
Design a class to serialize and deserialize an object
思路是什么呢?
char* ptr = reinterpret_cast(&Obj);
ofstream out("file", ios::binary|ios::out);
out.write(ptr, sizeof(Obj));
r*c
发帖数: 167
18
来自主题: JobHunting版 - 问个题,没思路
#include
#include
#include
#include
#include
using namespace std;
class KModulo {
public:
int numSolutions(const string& s, const int m, const int rmd, vector<
string>& res) {
int len = s.size();
int localM = m, mLen = 0;
while(localM){
localM /= 10, ++mLen;
}
if (len < mLen) return 0;
vectorvIndices;
unordered_mapmp; //[index of s, index of vIndices]
... 阅读全帖
f*******r
发帖数: 976
19
来自主题: JobHunting版 - 问一道airbnb的面试题
题目就是external sort的思想。统计单词的次数,明显用map。注意文
件很大,要用long,以免溢出。
在你读文件并且增加这个map时,内存不够用,这是你要把临时结果写入临时文件。你
可以设计一个threshold,比如1 billion,当map的size达到这个值时,你就把map和临
时文件merge到另一个临时文件里。最后再把这个文件rename到原来的临时文件。再把
map清空,继续读原文件直到结束。 C++代码如下:
// Split the string into words that consists of a..z and A..Z.
void split(const string &s, vector &res) {
int beg = -1;
for (int i = 0, e = s.size(); i < e; ++i) {
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) {
if (beg == ... 阅读全帖
L******k
发帖数: 395
20
my solution:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void jf_eval(string& in_line, unordered_map
>>& record)
{
int i_dex = in_line.find_first_of(' ');
string cur_var = in_line.substr(0, i_dex);
vector varibles;
int cur_value = 0, ele = 0;
bool new_variable = true, new_number = true; int index1 = 0;
int i = i_dex... 阅读全帖
j*****n
发帖数: 23
21
#include
#include
#include
#include
#include
#include
#include
using namespace std; //这个最好解释一下不会真的在production上这么写
void jf_eval(string& in_line, unordered_map
//jf 是什么意思?
>>& record)
{
int i_dex = in_line.find_first_of(' ');
string cur_var = in_line.substr(0, i_dex);
vector varibles;
int cur_value = 0, ele = 0;
bool new_variable = true, new_number = true; int ... 阅读全帖
g*****u
发帖数: 298
22
【 以下文字转载自 Programming 讨论区 】
发信人: grasssu (黄底黑花纹的猫), 信区: Programming
标 题: C++如何在linux上生成并使用超过2GB的大文件?
发信站: BBS 未名空间站 (Sun Aug 13 22:35:27 2006)
我的C++程序大概是:
ofstream outFile("file");
for(long i = 0; i< VERY_LARGE_NUMBER; i++)
outFile<<"something";
编译时使用了-DLINUX -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_
LARGEFILE64_SOURCE , 可是当超过2G时还是出错结束:
filesize limit exceeded.
程序在windows上运行正常。
谁知道怎么解决?谢谢。
j******x
发帖数: 383
23
来自主题: CS版 - about dcmtk
有人用过dcmtk吗?我在linux下用g++ 编译以后,用一个范例程序测试,编译的时候显
示:
In file included from /usr/local/include/dcmtk/ofstd/ofstring.h:65,
from /usr/local/include/dcmtk/oflog/tstring.h:27,
from /usr/local/include/dcmtk/oflog/streams.h:27,
from /usr/local/include/dcmtk/oflog/loglevel.h:29,
from /usr/local/include/dcmtk/oflog/logger.h:28,
from /usr/local/include/dcmtk/oflog/oflog.h:35,
from /usr/local/include/dcmtk/dcmdata/d... 阅读全帖
p*****d
发帖数: 81
24
首先我的经验是在win7上,也许xp上并不适用,仅供你参考。
* hardware: If my memory serves me, there were some SSDs "claiming" to have
sustained rate >300MB/s last year. You can compare the claimed speeds at: http://www.newegg.com/Store/SubCategory.aspx?SubCategory=636
For us, we want 1.1GB/s so single disk is not an option and we have to use
raid.
If you want to use rotational disks, you definitely need raid.
Try windows's software raid first. If it doesn't work for you, you can use
hardware raid or hardware+softwar... 阅读全帖
j******x
发帖数: 383
25
来自主题: Linux版 - about dcmtk (转载)
【 以下文字转载自 Software 讨论区 】
发信人: jackalex (峰之砂), 信区: Software
标 题: about dcmtk (转载)
发信站: BBS 未名空间站 (Tue Apr 12 14:48:51 2011, 美东)
发信人: jackalex (峰之砂), 信区: CS
标 题: about dcmtk
发信站: BBS 未名空间站 (Tue Apr 12 14:48:27 2011, 美东)
有人用过dcmtk吗?我在linux下用g++ 编译以后,用一个范例程序测试,编译的时候显
示:
In file included from /usr/local/include/dcmtk/ofstd/ofstring.h:65,
from /usr/local/include/dcmtk/oflog/tstring.h:27,
from /usr/local/include/dcmtk/oflog/streams.h:27,
from /usr/local... 阅读全帖
d*******d
发帖数: 2050
26
来自主题: Programming版 - c++ template中如何判断类型
用ofstream不就行了,不用你自己去判断类型。
l*****e
发帖数: 64
27
来自主题: Programming版 - c++ template中如何判断类型
是个好建议。
不过就是为了统一风格,程序中没有用到ofstream等作为文件输出。
l*****e
发帖数: 64
28
来自主题: Programming版 - c++ template中如何判断类型
i guess one reason maybe that:
it's more flexible to control the reading/writing using fprintf/sscanf than
ofstream, especially when working with a bit complex data.
n**d
发帖数: 9764
29
来自主题: Programming版 - indent C++ source code by VC++ 6.00
Thanks! How could you find it?! Is there any icon?
It works, but someting like this:
#define D(A) T << #A << endl; A
int main() {
ofstream T("format.out");
//assure(T);
D(int i = 47;)
D(float f = 2300114.414159;)
const char* s = "Is there any more?";
D(T.setf(ios::unitbuf);)
D(T.setf(ios::showbase);)
D(T.setf(ios::uppercase | ios::showpos);)
D(T << i << endl;) // Default is dec
...
}
t****t
发帖数: 6806
30
来自主题: Programming版 - ofstream and cout question
you can use
ostream& myout=cout;
but you can't change it. if you want to change it, use another scope.
T*****9
发帖数: 2484
31
来自主题: Programming版 - About command line in C++
VC++重定向比较困难
我一般都是
std::ofstream log("output.txt");
std::streambuf *oldbuf = std::cout.rdbuf(log.rdbuf());

to
h*******n
发帖数: 2052
32
比如 我有:
ofstream file;
我在很多函数里都需要输出到文件, file<<
如果每个函数都带上这个文件作为参数太麻烦了, 请问这个要怎么办呢?
h*******n
发帖数: 2052
33
can I declare the ofstream globally?
t****t
发帖数: 6806
34
来自主题: Programming版 - Why no output file generate? What is wrong?
since you used fstream, the open mode is 0 by default. you have to
explicitly say
fout.open("...", ios_base::out);
or alternatively, use
ofstream fout;
fout.open("...");
EDIT: the default open mode is in|out by default. seems it's not stated
on standard, but i guess most implementation will do that. however, if
you specify ios_base::in, and file do not exist, the open will fail anyway.
E*V
发帖数: 17544
35
来自主题: Programming版 - Why no output file generate? What is wrong?
ofstream is better a
t****e
发帖数: 69
36
来自主题: Programming版 - 怎么快速填充一个文件
我要生成一个固定大小的文件,大概几个GB,内容无所谓,可以全是0或者任何垃圾,主
要就是占地方。现在我这么写:
std::ofstream ofs("junk.dat");
for(streamoff i = 0; i < size; ++i) ofs.put('\0');
ofs.close();
但是好像很慢,估计for loop占了不少时间。怎么可以优化一下?
s*****w
发帖数: 1527
37
1. for this code,
ostream& operator << (ostream& s, int cnt)
{
s << cnt;
return s;
}
i got
'operator <<' is ambiguous
why ? this is in VS 2005
if i change "int cnt" to "char c", it compiles.
2. also why ostream << overload has to return "ostream&" not "void" ?
i thought i'm passing in ostream&s anyway,
so when i use
ofstream f("myfile");
f<<55;
i don't see why the return "ostream &" got used.
many thanks !
z****e
发帖数: 2024
38
来自主题: Programming版 - C++默认的copy constructor的疑惑
看到thinking in c++一段代码:
#include
#include
using namespace std;
ofstream out("HowMany.out");
class HowMany {
static int objectCount;
public:
HowMany() { objectCount++; }
static void print(const string& msg = "") {
if(msg.size() != 0) out << msg << ": ";
out << "objectCount = "
<< objectCount << endl;
}
~HowMany() {
objectCount--;
print("~HowMany()");
}
};
int HowMany::objectCount = 0;
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
}
z****e
发帖数: 2024
39
来自主题: Programming版 - 问一个C++文件读取的问题
int i, k;
//Create a file
ofstream OUTPUT( "RESULT.dat"); //ios::out | ios::in );
if( !OUTPUT ){
cerr << "File could not be opened." << endl;
exit( 1 );
}
//Input the data
for( i=1; i<=8; i++ ){
k = i*10;
cout << " k = " << k << endl;
OUTPUT.write( reinterpret_cast( &k ), sizeof(int) );
}
OUTPUT.close();
cout << endl << "Start Output" << endl;
ifstream INPUT("RESULT.dat");
INPUT.seekg( 2*sizeo
D******4
发帖数: 47
40
来自主题: Programming版 - 请问一个入门级 dynamic memory 的问题
今天看到dynamic memory,测试如下程序:
#include
#include
#include
using namespace std;
ifstream fin("test.in");
ofstream fout("test.out");
int main (){
int i = 0;
int n = 0;
int* p;

if(fin.is_open()){
fin>>n;
fout<<"n is: "< p = new (nothrow) int[n];
if (p == 0)
fout << "Error: memory could not be allocated"< else{
for (i=0; i> p[i];
fin.close();
fout << "You have ent
M*******r
发帖数: 165
41
来自主题: Programming版 - 借人气问个c++的overflow (转载)
【 以下文字转载自 Quant 讨论区 】
发信人: Morphiner (Ninja Turtle), 信区: Quant
标 题: 借人气问个c++的overflow
发信站: BBS 未名空间站 (Mon Nov 15 12:40:52 2010, 美东)
stack overflow在main后面的{
会是怎么回事?
附上code:
//#include
#include
#include
#include
#include "BarGame.h"
#include
#include
//#include "randomc.h"
//#include "mersenne.cpp" // code for random number generator of
type Mersenne twister
#include "require.h"
//#inclu... 阅读全帖
t****t
发帖数: 6806
42
no, most ppl don't need that. just know istream and ostream and their
variations is enough (istream is basic_istream, similar for ostream).
their variations include ifstream/ofstream, istringstream/ostringstream.
just know op<< for output, op>> for input, op! (usually you need that
instead of eof() member) to check eof, clear() for opening another file,
manipulators (such as setw(), endl, hex, etc.) for formating, and getline()
for reading a whole line. these are enough for 90% of ppl deal... 阅读全帖
w***g
发帖数: 5958
43
来自主题: Programming版 - C++ ofstream binary output slow
怎么搞就只能搞到30MB/s的速度。7200RPM的硬盘,裸写应该可以达到100MB/s。用stra
ce发现底层是用writev实现的,每次只写8KB多一点。即使用了rdbuf()->pubsetbuf也不
行。改成FILE和fwrite后,默认也是每次写8KB多一点(BUFSIZ=8KB),但是setvbuf后就
可以buffer任意大了。这次本牛也搞不定了,希望版上的大牛们给支援一下。
b***i
发帖数: 3043
44
来自主题: Programming版 - C++ ofstream binary output slow
因素很多,比如磁盘的扇区大小,还有你是不是读了又写,磁盘测试软件结果怎么样?
可以预先设定文件大小,文件是不是在硬盘上连续,cpu如果不写磁盘光准备数据有 多
块?
文件到底多大?在windows还是linux?

stra
也不
n******t
发帖数: 4406
45
来自主题: Programming版 - C++ ofstream binary output slow
C++ 的stream 本来效率就低下。。。一般用用就行了。

stra
也不
s*****V
发帖数: 21731
46
来自主题: Programming版 - C++ ofstream binary output slow
专业的写用什么?
t****t
发帖数: 6806
47
来自主题: Programming版 - C++ ofstream binary output slow
basic_filebuf::setbuf()除了在buffer是null的情况下确定无buffer, 别的都是
implementation-defined.
我看了下源码, basic_filebuf内部用的是basic_file<>, which is a wrapper for
FILE*. 那个内部另有buffer, 如果只靠标准函数似乎拿不到. 我看看能不能用非标准
的东西来拿.

stra
也不
t****t
发帖数: 6806
48
来自主题: Programming版 - C++ ofstream binary output slow
OK, after some googling, I think you can use:
#include
__gnu_cxx::stdio_filebuf
to replace std::basic_filebuf<>. This can be constructed from a FILE*, and
you can then do whatever to that FILE* (setvbuf, for example).

stra
也不
w***g
发帖数: 5958
49
来自主题: Programming版 - C++ ofstream binary output slow
好吧。既然你们这几个老水车都只能想出来这个办法,我还是直接FILE*算了。也不是非
要为了C++而C++。看来C++的iostream确实效率低下。
t****t
发帖数: 6806
50
来自主题: Programming版 - C++ ofstream binary output slow
这个确实不是为效率设计的, 不过它其实也没有太多大问题. 的思想是, 把字
节流的转换(stream层)和I/O(streambuf)分开了, 这个本身是很不错的思想.

是非
1 (共1页)