ofstream

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

拟墨画扇 提交于 2020-01-23 01:50:08
问题 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, 0x1C}; Byte major = 0; Byte minor = 3; ofs.write((const char*)signature, 6); ofs.write((const char*)major, 1); ofs.write((const char*)minor, 1); UInt64 offset =

Is it possible to pass cout or fout to a function?

烂漫一生 提交于 2020-01-19 05:37:27
问题 I'm trying to find a way to pass fout or cout to a function. I realize there are logically easy ways to deal with this, like put ifs in any function that outputs data or even just write the function both ways. However, that seems primitive and inefficient. I don't believe this code would ever work, I'm putting it here to ensure it's easy to see what I'd "like" to do. Please be aware that I'm taking a algorithm design class using c++, I'm in no way a seasoned c++ programmer. My class is

C++, regarding fprintf and ofstream

那年仲夏 提交于 2020-01-13 09:56:30
问题 I've been using fprintf for a while now and I'd like to ask a question. What is the equivalent of this fprintf line: fprintf(OutputFile, "%s", "SomeStringValue"); using ofstream ? How to use the "%s" in ofstream is what I'd really like to know. How to take the next argument and print it as a string? 回答1: You don't use it. The equivalent is essentially: std::ofstream x("your_file"); x << "SomeStringValue"; x.close(); Go read about it on any of several reference pages. Such as http://www

Writing multiple array pointers to file with ofstream?

╄→гoц情女王★ 提交于 2020-01-06 04:25:52
问题 I'm having some seriously strange trouble writing multiple arrays of data to a file. Basically, I'm wanting to store all the array sizes at the top of the file, and then the array data following. This way I can just read the sizes and use that to construct arrays to hold the data on import, and I'll know exactly where each array begins and ends. Here's the problem: I write the data, but it's different on import. Please take a look at my little test code. At the bottom there are comments about

ofstream doesn't open, or write to files

人走茶凉 提交于 2020-01-05 07:04:39
问题 I've been looking at this for hours and I just know the answer is simple. It seems no matter what I do I cannot open a file. It's a multi-class program so in the header I have #include <iostream> #include < fstream> class A{ string path; A(string p): path(p){} ... ... void PrintToFile(); void PrintBase(); void PrintNext(); ... ... }; and in the cpp file I have #include "A.h" void A::PrintToFile(){ ofstream f(path.c_str(), ios::out); assert(f.is_open); f << "markuptext" << endl; PrintBase(); f

I'd like to use ifstream and ofstream in C++ to mimic C#'s BinaryReader / BinaryWriter functionality

女生的网名这么多〃 提交于 2020-01-05 04:34:18
问题 I'm looking for a way to write floats/ints/strings to a file and read them as floats/ints/strings. (basically read/write as ios::binary). 回答1: I subclassed ifstream and ofstream : ibfstream and obfstream . I made a little helper class that would detect the endianness of the machine I was compiling/running on. Then I added a flag for ibfstream and obfstream that indicated whether bytes in primitive types should be flipped. These classes also had methods to read/write primitive types and arrays

How can I redirect a std::ofstream to std::cout?

假如想象 提交于 2020-01-03 14:18:11
问题 I'm working with some code that uses a global debug logger that is of type std::ofstream* . I would like to redirect this to std::cout since I'm using the code in realtime, as opposed to a batch method for which it was designed. Is it possible to redirect the global std::ofstream* pointer it uses to std::cout ? I know std::ofstream inherits from std::ios , which allows one to change the stream buffer using the rdbuf() method, but unfortunately it appears std::ofstream redefines the rdbuf()

How to create a text file in a folder on the desktop

谁说胖子不能爱 提交于 2020-01-02 07:56:46
问题 I have a problem in my project. There is a project folder on my desktop. I want to create a text file and write something include this text file. That is my code: ofstream example("/Users/sample/Desktop/save.txt"); But I want to it could been run the other mac. I don't know what I should write addres for save.txt. Can anyone help me? 回答1: Create a file and write some text to it is simple, here is a sample code: #include <iostream> #include <fstream> #include <string> using namespace std; int

How to create a text file in a folder on the desktop

微笑、不失礼 提交于 2020-01-02 07:56:09
问题 I have a problem in my project. There is a project folder on my desktop. I want to create a text file and write something include this text file. That is my code: ofstream example("/Users/sample/Desktop/save.txt"); But I want to it could been run the other mac. I don't know what I should write addres for save.txt. Can anyone help me? 回答1: Create a file and write some text to it is simple, here is a sample code: #include <iostream> #include <fstream> #include <string> using namespace std; int

Difference between casting ifstream to bool and using ifstream::is_open()

此生再无相见时 提交于 2020-01-01 10:04:13
问题 Maybe a dummy question, but I need a clear answer to it. Is there any difference at all in the return of any of those functions int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return !!file; } int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return file.is_open(); } So in other words, my question is: does casting the fstream to bool give exactly the same result as fstream::is_open()? 回答1: No. is_open checks only whether there is an