ofstream

Writing stringstream contents into ofstream

I'm currently using a std::ofstream a function and a std::stringstream std::ofstream outFile; outFile.open(output_file); Then I call a function GetHolesResults(..., std::ofstream &outFile){ float x = 1234; std::stringstream ss; ss << x << std::endl; outFile << ss; } Now my outFile contains nothing but garbage "0012E708" re...

Assigning cout to a variable name

In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send it to the screen. So something like: ofstream outFile; if (outFileRequested) outFile.open("foo.txt", ios::out); else outFile = cout; // Will not compile bec...

How do I write binary data for 7z archive format?

I've been pouring over the format description and source code for the 7z archive format, but I'm still having trouble writing a valid container. I assume I can create an empty container... anyway here's my start: std::ofstream ofs(archivename.c_str(), std::ios::binary|std::ios::trunc); Byte signature[6] = {'7', 'z', 0xBC, 0xAF, 0x27,...

ofstream error

This is in reference to another question I asked, though it is its own question entirely. when I compile I get two errors: 1>.\asst4.cpp(73) : error C2065: 'outfile' : undeclared identifier 1>.\asst4.cpp(73) : error C2228: left of '.close' must have class/struct/union I'm slightly confused as to what I've done incorrectly here? Any r...

How does one write the hex values of a char in ASCII to a text file?

Here is what I currently have so far: void WriteHexToFile( std::ofstream &stream, void *ptr, int buflen, char *prefix ) { unsigned char *buf = (unsigned char*)ptr; for( int i = 0; i < buflen; ++i ) { if( i % 16 == 0 ) { stream << prefix; } stream << buf[i] << ' '; } } I've tried doing stream.hex, str...

How to do fsync on an ofstream?

I want to make sure that an ofstream has been written to the disk device. What's the portable way (portable on POSIX systems) of doing this? Does that solve the problem if I open the file separately in read-only append mode to get a file descriptor and call fsync with it? Like this: ofstream out(filename); /* ... write content into ...

Write a circular file in c++

Hi, I need to write a circular file in c++. The program has to write lines in a file and when the code reaches a maximum number of lines, it must overwrite the lines in the beginning of the file. Anyone have any idea? Thanks ...

writing files and dirs using C++

I'm working on a program that creates 2000 directories and puts a file in each (just a 10KB or so file). I am using mkdir to make the dirs and ofstream (i tried fopen as well) to write the files to a solid state drive (i'm doing speed tests for comparison). When I run the code the directories are created fine but the files stop writing ...

How to easily indent output to ofstream?

Hi, Is there an easy way to indent the output going to an ofstream object? I have a C++ character array that is null terminate and includes newlines. I'd like to output this to the stream but indent each line with two spaces. Is there an easy way to do this with the stream manipulators like you can change the base for integer output ...

C++ ofstream vs. C++ cout piped to file

I'm writing a set of unit tests that write calculated values out to files. Each test produces a square matrix that holds anywhere from 50,000 to 500,000 doubles, and I have a total of 128 combinations of test cases. Is there any significant overhead involved in writing cout statements and then piping that output to files, or would I be...

Dynamic output filenames (C++)

I'm trying to create output files subscripted by a dynamic index ( d = {0,...,NUM_DEMES-1}). Currently, I'm only getting output files for the first value (d=0). #include <sstream> #include <string> void Simulation::updateSimulation( double t ) { ... ofstream abundanceStream; ofstream abHeaderStream; if ( step == 1 ) { for ...

why are special characters read false from file

Hi my program saves some settings (mostly string) to a text file, to retrieve them later, but alas! The special characters come back unrecognizable! saveSettings saves the strings one by one... void email::saveSettings(string filename){ ofstream savefile(filename.c_str(),ios::out | ios::trunc); email settingsemail(this); s...

Partially truncating a stream (fstream or ofstream) in C++

Hi, I am trying to partially truncate (or shorten) an existing file, using fstream. I have tried writing an EOF character, but this seems to do nothing. Any help would be appreciated... ...

c++ std::ofstream flush() but not close()

I'm on MacOSX. In the logger part of my application, I'm dumping data to a file. suppose I have a globally declared std::ofstream outFile("log"); and in my logging code I have: outFile << "......." ; outFile.flush(); Now, suppose my code crashes after the flush() happens; Is the stuff written to outFile before the flush() guarantee...

Returning ifstream in a function

Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I need to create that file for the user. I know how to do that, but only by asking the user ...

C++ - Unwanted characters printed in output file.

This is the last part of the program I am working on. I want to output a tabular list of songs to cout. And then I want to output a specially formatted list of song information into fout (which will be used as an input file later on). Printing to cout works great. The problem is that tons of extra character are added when printing to fo...

Write set of integers to std::ofstream and be able to read them back

Hello, I need to write a bunch of unsigned integers to std::ofstream in binary mode: std::ofstream f; f.open("some path", std::ios::out | std::ios::binary); // some loop { unsigned int k = get_k(); // may product numbers from 0 to 65535 f << k; } f.close(); They are written to the output file "as is" w/o any delimiter. So when I'...

C++ ofstream cannot write to file....

Hey I am trying to write some numbers to a file, but when I open the file it is empty. Can you help me out here? Thanks. /** main function **/ int main(){ /** variables **/ RandGen* random_generator = new RandGen; int random_numbers; string file_name; /** ask user for quantity of random number to produce **/ ...

How do you search a document for a string in c++?

Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() { int count = 0; string fileName; string keyWord; string word; cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ; ...

C++ iostream not setting eof bit even if gcount returns 0

Hi I'm developping an application under windows, and i'm using fstreams to read and write to the file. I'm writing with fstream opened like this : fs.open(this->filename.c_str(), std::ios::in|std::ios::out|std::ios::binary); and writing with this command fs.write(reinterpret_cast<char*>(&e.element), sizeof(T)); closing the file a...