fstream

Relative path with fstream c++

最后都变了- 提交于 2019-12-10 15:16:24
问题 I try to load a file with fstream. The code looks like this file.open("../levels/level0.lvl"); if (file.is_open()) { while (!file.eof()) { std::getline(file, Str); list = ReadLine(Str, list); } } But it loads nothing. Yes only if the path is absolute. How can I make the path relative? The folder "levels" is hosted in the debug folder. same folder as the exe. 回答1: "The folder "levels" is hosted in the debug folder. same folder as the exe." It doesn't matter in which position the levels folder

C++ Decorate basic_iostream classes

半城伤御伤魂 提交于 2019-12-10 14:18:50
问题 I want to do something like the following code shows: class foo { private: std::fstream* m_stream; public: foo(std::fstream* stream) : m_stream(stream) { } foo& write(char const* s, std::streamsize count) { if (/*condition*/) { m_stream->write(s, count); } else { // ... } return *this; } foo& read(char* s, std::streamsize count) { if (/*condition*/) { m_stream->read(s, count); } else { // ... } return *this; } }; I would need to add the same behavior to all similar methods (e.g. put ). This

What does std::ofstream::close() actually do?

蓝咒 提交于 2019-12-10 13:19:52
问题 This question: How to protect log from application crash? has lead me to another - what does std::ofstream::close() actually do? I know it calls flush() and that's one thing. But what else? What closing the file actually is? Edit: Let me rephrase my question - is anything physically done to the actual file during the call to close() or is it just std::ofstream internal cleanup stuff? 回答1: Other than flushing the userspace buffers, i.e. flush() , close(2) is called on the underlying file

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_)

read multiple files with quite similar names c++

梦想的初衷 提交于 2019-12-10 12:25:49
问题 I am reading a file from current directory ifstream myfile; myfile.open("version1.1.hex"); Now a situation is arising that if user updates version then there will be version1.2.hex or version1.3.hex ..so on in the current directory, but one file at a time will be present. I want to write a code now which will cater this future need of reading different file. I'm writing this code in C++/CLI. 回答1: Since file listings are a bit environment-dependant I am not sure if this is helpful to you, but

really easy c++: Does the operator >> in fstream remove newline characters after reading something?

孤者浪人 提交于 2019-12-10 10:28:45
问题 // file.in 12 13 // main.cpp fstream f("file.in", ios::in); int n; char c; f >> n; f.get(&c); After extracting the number 12, what is the next character? Is it newline or '1'? If I call getline instread of get, do I get an empty line or '13'? 回答1: It leaves the delimiter in the input buffer, so the next character you read will be a new-line. Note, however, that most extractors will skip white space (which includes new-line ) before anything they extract, so unless you do call something like

C++ binary files and iterators: getting away with a 1:1 using ifstreambuf_iterator?

前提是你 提交于 2019-12-10 05:33:31
问题 This answer points out the fact that C++ is not well suited for the iteration over a binary file, but this is what I need right now, in short I need to operate on files in a "binary" way, yes all files are binary even the .txt ones, but I'm writing something that operates on image files, so I need to read files that are well structured, were the data is arranged in a specific way. I would like to read the entire file in a data structure such as std::vector<T> so I can almost immediately close

Why is my fstream being implicitly deleted?

安稳与你 提交于 2019-12-10 02:19:02
问题 I'm working with a few HID devices, all of which have classes deriving from the following base class (in main.h ): class HIDDevice { public: hid_device *device; virtual void read(std::fstream)=0; virtual void write(std::fstream)=0; }; Here's one of the device classes deriving from it ( device.h ): class MyDevice : public HIDDevice { public: void read(std::fstream); void write(std::fstream); }; ...and a sample of the implementation: void MyDevice::read(std::fstream file) { // Read from card

how do I override a std::filebuf?

会有一股神秘感。 提交于 2019-12-09 23:08:00
问题 I have a Visual Studio 2008 C++ 03 application using STLPort 5.2.1 where I would like to use a custom std::filebuf implementation. For example: class MyFileBuf : public std::filebuf { protected: virtual int_type sync() { // breakpoint here never fires return std::filebuf::sync(); }; virtual std::streamsize xsputn( const char_type* p, std::streamsize n ) { // breakpoint here never fires return std::filebuf::xsputn( p, n ); }; virtual int_type overflow( int_type c = traits_type::eof() ) { //

Seeking in large files with ifstream

∥☆過路亽.° 提交于 2019-12-09 17:31:58
问题 I'm implementing a program in C++ using ifstream that must seek in large files (~1TB). However, this fails after reading 2GB. Is there a way to get file positions, even for large files? I compile for a 32-bit windows machine. std::ifstream f; f.open( filename.c_str(), std::ifstream::in | std::ifstream::binary ); while(true) { std::cout << (uint64_t)(f.tellg()) << std::endl; //read data } 回答1: Since you are compiling on a 32-bit platform, if you use fstream , you are going to get 32-bits