I use c++ stream to write content into file, and i found only when file size hit 4k*n, the content will be written into file, else it will miss, can anyone explain this?
please see the following code:
#include "../order.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
std::ofstream f;
f.open("test.log", ios::out); // | ios::app);// | ios::binary);
while (cin >> s && s != "quit") {
Order o;
snprintf(o.contract, sizeof(o.contract), "%s", s.c_str());
o.Show(f);
}
f.close();
}
order.h
#include <stdio.h>
#include <sys/time.h>
#include <fstream>
struct Order {
timeval shot_time;
timeval send_time;
char contract[MAX_CONTRACT_LENGTH];
double price;
int size;
int traded_size;
OrderSide::Enum side;
char order_ref[MAX_ORDERREF_SIZE];
OrderAction::Enum action;
OrderStatus::Enum status;
Offset::Enum offset;
char tbd[128];
Order()
: size(0),
traded_size(0),
offset(Offset::UNINITED) {
snprintf(tbd, sizeof(tbd), "%s", "null");
}
void Show(std::ofstream &stream) const {
stream.write((char*)this, sizeof(*this));
}
}
when i loop to cin, file size added only when content is 4k*n, how can i avoid the miss of message?