fstream

Troubles using fstream in a class

匆匆过客 提交于 2020-01-24 12:11:54
问题 I get the following error, when compiling: 1>c:\users\ra\source\repos\sandbox\game\gamesetup_1\gamesetup_1\main.cpp(15): error C2280: 'DebugLib::DebugLib(const DebugLib &)': attempting to reference a deleted function 1>c:\users\ra\source\commonincludes\tannic\debuglib\debuglib.h(41): note: compiler has generated 'DebugLib::DebugLib' here 1>c:\users\ra\source\commonincludes\tannic\debuglib\debuglib.h(41): note: 'DebugLib::DebugLib(const DebugLib &)': function was implicitly deleted because a

C++ std::istream readsome doesn't read anything

随声附和 提交于 2020-01-23 05:52:13
问题 It's like readsome isn't even reading. Returns 0 and doesn't read any chars. What is wrong here? #include <fstream> #include <iostream> int main () { std::fstream stream("list.cpp", std::ios::in); if (stream.good() || !stream.bad() || stream.is_open()) { std::cout << "Well, stream looks good." << std::endl; char justOneChar = 'L'; auto ssize = stream.readsome(&justOneChar, 1); std::cout << ssize << " : " << justOneChar << std::endl; } return -1; } Output: Well, stream looks good. 0 : L 回答1:

std::getline throwing when it hits eof

a 夏天 提交于 2020-01-21 04:48:47
问题 std::getline throws exception when it gets an eof . this is how I am doing. std::ifstream stream; stream.exceptions(std::ifstream::failbit|std::ifstream::badbit); try{ stream.open(_file.c_str(), std::ios_base::in); }catch(std::ifstream::failure e){ std::cout << "Failed to open file " << _file.c_str() << " for reading" << std::endl; } while(!stream.eof()){ std::string buffer = ""; std::getline(stream, buffer); //process buffer //I do also need to maintain state while parsing } In the above

std::getline throwing when it hits eof

雨燕双飞 提交于 2020-01-21 04:48:45
问题 std::getline throws exception when it gets an eof . this is how I am doing. std::ifstream stream; stream.exceptions(std::ifstream::failbit|std::ifstream::badbit); try{ stream.open(_file.c_str(), std::ios_base::in); }catch(std::ifstream::failure e){ std::cout << "Failed to open file " << _file.c_str() << " for reading" << std::endl; } while(!stream.eof()){ std::string buffer = ""; std::getline(stream, buffer); //process buffer //I do also need to maintain state while parsing } In the above

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

What is the correct way to output hex data to a file?

北城余情 提交于 2020-01-17 05:05:32
问题 I've read about [ostream] << hex << 0x[hex value] , but I have some questions about it (1) I defined my file stream, output , to be a hex output file stream, using output.open("BWhite.bmp",ios::binary); , since I did that, does that make the hex parameter in the output<< operation redundant? (2) If I have an integer value I wanted to store in the file, and I used this: int i = 0; output << i; would i be stored in little endian or big endian? Will the endi-ness change based on which computer

What is the correct way to output hex data to a file?

我是研究僧i 提交于 2020-01-17 05:05:28
问题 I've read about [ostream] << hex << 0x[hex value] , but I have some questions about it (1) I defined my file stream, output , to be a hex output file stream, using output.open("BWhite.bmp",ios::binary); , since I did that, does that make the hex parameter in the output<< operation redundant? (2) If I have an integer value I wanted to store in the file, and I used this: int i = 0; output << i; would i be stored in little endian or big endian? Will the endi-ness change based on which computer

Reading integers from a file with mixed integers, letters, and spaces C++

余生颓废 提交于 2020-01-16 18:48:29
问题 This is a sort of self-imposed extra credit problem I'm adding to my current programming assignment which I finished a week early. The assignment involved reading in integers from a file with multiple integers per line, each separated by a space. This was achieved easily using while(inFile >> val) . The challenge I put myself up to was to try and read integers from a file of mixed numbers and letters, pulling out all contiguous digits as separate integers composed of those digits. For

C++ Binary File I/O Operations Slow Down… How DB Handle Binary Files?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-16 09:06:42
问题 I am trying to make a simple file-based hash table. Here is my insert member function: private: std::fstream f; // std::ios::in | std::ios::out | std::ios::binary public: void insert(const char* this_key, long this_value) { char* that_key; long that_value; long this_hash = std::hash<std::string>{}(this_key) % M; long that_hash; // also block status long block = this_hash; long offset = block * BLOCK_SIZE; while (true) { this->f.seekg(offset); this->f.read((char*) &that_hash, sizeof(long)); if

How can I open a file in c++, without erasing its contents and not append it?

本小妞迷上赌 提交于 2020-01-16 08:46:11
问题 I am trying to find a way using which I can Edit the contents in a binary file, without reading the whole file. Suppose this is my file abdde And I want to make it abcde I tried following:- Attempt 1) ofstream f("binfile", ios::binary); if(f.is_open()){ char d[]={'c'}; f.seekp(2,ios::beg); f.write(d, 1); f.close(); } //the file get erased Output: **c Attempt 2) ofstream f("binfile", ios::binary | ios::app); if(f.is_open()){ char d[]={'c'}; f.seekp(2,ios::beg); f.write(d, 1); f.close(); } /