ofstream

Write last half of the vector to std::ofstream

强颜欢笑 提交于 2019-12-10 12:26:30
问题 I am writing code to write a vector to file. My aim is to write the last half of the vector to file first and then the first half based on offset. The code below gives me segmentation fault. std::vector<uint8_t> buffer_(1000); // the vector is filled with values int offset_ = 300; std::ofstream output_file (file_name.c_str(), std::ofstream::out | std::ofstream::binary); if (output_file.is_open()) { output_file.write(reinterpret_cast<const char*>(&buffer_[offset_]), (buffer_.size() -offset_)

Avoid contents of an existing file to be overwritten when writing to a file

本秂侑毒 提交于 2019-12-09 04:42:52
问题 I am trying to make a game that implements high scores into a .txt file. The question I have is this : when I make a statement such as: ofstream fout("filename.txt"); Does this create a file with that name, or just look for a file with that name? The thing is that whenever I start the program anew and make the following statement: fout << score << endl << player; it overwrites my previous scores! Is there any way for me to make it so that the new scores don't overwrite the old ones when I

C++ vector of ofstream, how to write to one particular element

微笑、不失礼 提交于 2019-12-08 10:44:36
问题 To avoid continuously opening and closing multiple files that I am writing to, I am attempting to use a vector of ofstream objects. My code looks like the following so far: std::vector<shared_ptr<ofstream>> filelist; void main() { for(int ii=0;ii<10;ii++) { string filename = "/dev/shm/table_"+int2string(ii)+".csv"; filelist.push_back(make_shared<ofstream>(filename.c_str())); } } I am using a vector of ofstream pointers because ofstream doesn't have a copy constructor. Furthermore, I'm using a

How to increase buffer size of ofstream

淺唱寂寞╮ 提交于 2019-12-08 08:54:11
问题 I wanted to increase the buffer size of a c++ program so that it does not write too frequently. The default buffer is 8192 bytes, I tried to increase it to 200K with pubsetbuf. The original code ofstream fq(fastq1.cstr(), ios::out); // fastq1 is a file BamReader reader; // BamReader parses and reads in "bamFile" below reader.Open(bamFile); while (reader.GetNext()) { fq << bam.Name << "\n"; fq << bam.seq << "\n"; fq << bam.qual << "\n"; } Modified ofstream fq(fastq1.cstr(), ios::out); //

why std::wofstream do not print all wstring into file?

折月煮酒 提交于 2019-12-07 17:38:52
问题 I have a std::wstring whose size is 139,580,199 characters. For debugging I printed it into file with this code: std::wofstream f(L"C:\\some file.txt"); f << buffer; f.close(); After that noticed that the end of string is missing. The created file size is 109,592,584 bytes (and the "size on disk" is 109,596,672 bytes). Also checked if buffer contains null chars, did this: size_t pos = buffer.find(L'\0'); Expecting result to be std::wstring::npos but it is 18446744073709551615 , but my string

Array of Ofstream in c++

蓝咒 提交于 2019-12-07 08:32:31
问题 I want 41 output files to use in my project to write text on them. first create a string array list to name those output files then I tried to define an array of ofstream objects and use list to name them, but I get this error that 'outfile' cannot be used as a function . Below is my code: #include <sstream> #include <string> #include <iostream> #include <fstream> using namespace std ; int main () { string list [41]; int i=1; ofstream *outFile = new ofstream [41]; for (i=1;i<=41 ;i++) {

Change or check the openmode of a std::ofstream

梦想与她 提交于 2019-12-07 04:34:03
问题 In some code that does a lot of file i/o using std::ofstream , I'm caching the stream for efficiency. However, sometimes I need to change the openmode of the file (e.g. append vs truncate). Here is some similar mock code: class Logger { public: void write(const std::string& str, std::ios_base::openmode mode) { if (!myStream.is_open) myStream.open(path.c_str(), mode); /* Want: if (myStream.mode != mode) { myStream.close(); myStream.open(path.c_str(), mode); } */ myStream << str; } private: std

How to increase buffer size of ofstream

耗尽温柔 提交于 2019-12-06 21:27:28
I wanted to increase the buffer size of a c++ program so that it does not write too frequently. The default buffer is 8192 bytes, I tried to increase it to 200K with pubsetbuf. The original code ofstream fq(fastq1.cstr(), ios::out); // fastq1 is a file BamReader reader; // BamReader parses and reads in "bamFile" below reader.Open(bamFile); while (reader.GetNext()) { fq << bam.Name << "\n"; fq << bam.seq << "\n"; fq << bam.qual << "\n"; } Modified ofstream fq(fastq1.cstr(), ios::out); // fastq1 is a file with certain format // setting buffer size to 200K int buf_size = 204800; char buf[buf_size

Defining fstream inside a 'if' conditional

断了今生、忘了曾经 提交于 2019-12-06 20:23:54
问题 In an answer there was the following code: if (std::ifstream input("input_file.txt")) ; Which seems convenient, limiting the scope of the 'input' variable to where it's confirmed to be valid, however neither VS2015 nor g++ seems to compile it. Is it some compiler specific thing or does it require some extra flags? In VS2015 the IDE highlights "std::ifstream" and the "input_file.txt" as well as the last parentheses. "std::ifstream" is flagged with "Error: a function type is not allowed here".

Issue on writing wstring to a file for hebrew/arabic language

笑着哭i 提交于 2019-12-06 07:15:43
I want to read hebrew(unicode) using xerces parser. I am able to read the value in XMLCh. However, while writing it to another file I get gargabe value. I tried using ofstream, wofstream but didnot helped. Let me know your suggestions The problem with wofstream is that it accepts the wide string for the open() method but does not actually write wide characters to the file. You have to be explicit about that and imbue() it with a locale that has a codecvt with the encoding you want. Implementation of such a codecvt that produces a UTF encoding is still spotty, here's an example that uses Boost.