fstream

c++ Reading string from binary file using fstream

我只是一个虾纸丫 提交于 2019-12-22 16:43:22
问题 I am trying to read a string from a binary file but cant seem to get it to work. I am a pretty new to c++. Can anybody help please? Thanks. string Name = "Shaun"; unsigned short int StringLength = 0; int main() { StringLength = Name.size(); ofstream oFile("File.txt", ios::binary|ios::out); oFile.write((char*)&StringLength, sizeof(unsigned short int)); oFile.write(Name.c_str(), StringLength); oFile.close(); StringLength = 0; Name = "NoName"; ifstream iFile("File.txt", ios::binary|ios::in); if(

c++ Reading string from binary file using fstream

断了今生、忘了曾经 提交于 2019-12-22 16:43:11
问题 I am trying to read a string from a binary file but cant seem to get it to work. I am a pretty new to c++. Can anybody help please? Thanks. string Name = "Shaun"; unsigned short int StringLength = 0; int main() { StringLength = Name.size(); ofstream oFile("File.txt", ios::binary|ios::out); oFile.write((char*)&StringLength, sizeof(unsigned short int)); oFile.write(Name.c_str(), StringLength); oFile.close(); StringLength = 0; Name = "NoName"; ifstream iFile("File.txt", ios::binary|ios::in); if(

fstream ifstream I don't understand how to load a data file into my program

℡╲_俬逩灬. 提交于 2019-12-22 08:32:55
问题 My professor is very smart but expects complete noobs like me to just know how to program c++. I don't understand how the fstream function works. I will have a data file with three columns of data. I will have to determine with a logarithm whether each line of data represents a circle, rectangle or triangle - that part is easy. The part I don't understand is how the fstream function works. I think I: #include < fstream > then I should declare my file object? ifstream Holes; then I open it:

Processing files larger than 2 GB in C++ with STL

混江龙づ霸主 提交于 2019-12-22 04:37:38
问题 I am doing binary file processing and in my algorithm I would like to know the actual type of pos_type and off_type , for example when computing the size of the file or seeking to a given position ( tellg and seekg ). When computing the size of the file I just static_cast the pos_type to an int64_t and it seems to work fine. How about seekg ? Is it safe to pass an int64_t to it? Is there a way to make pos_type and off_type to be an int64_t , perhaps using traits ? I would like to eliminate

How to protect log from application crash?

一曲冷凌霜 提交于 2019-12-21 20:58:22
问题 I've created a simple logger which, well, logs everything of importance into a text file. I'm using std::ofstream but there's a problem - when the program doesn't close the file (call std::ofstream::close() ) for whatever reason (like crash), the created log is actually empty (0 size). And since the log is most useful in exceptional situations (when something goes wrong) - you see the problem. Is there any way to protect my log from that? I could try closing the file after writing every

C++: .eof on an empty file

半腔热情 提交于 2019-12-21 05:45:09
问题 Lets see this program: ifstream filein("hey.txt"); if(filein.eof()){ cout<<"END"<<endl; } Here "hey.txt" is empty. So the if condition here is thought should have been true But it isnt Why isnt the eof returning true although the file is empty? If i added this before the if the eof returns true although arr is still empty and the file is still empty so both unchanged char arr[100]; filein.getline(arr,99); 回答1: eof() function returns "true" after the program attempts to read past the end of

Does std::ofstream truncate or append by default?

假如想象 提交于 2019-12-21 03:32:45
问题 If you call the std::ofstream constructor without openmode flags, the default flag is ios_base::out. But does this imply ios_base::trunc or ios_base::app ? In other words, if you already have a non-empty file "past.txt" in your file system and you call std::ofstream stream( "past.txt" ); stream << "new content"; will "new content" be appended to the prior contents of "past.txt" or will it replace the prior contents? 回答1: The short version It truncates by default. The medium version The

Why am I getting this ifstream error?

无人久伴 提交于 2019-12-20 16:14:39
问题 Implicit instantiation of undefined template 'std::basic_ifstream<char,std::char_traits<char>>' #ifndef MAPPER_H #define MAPPER_H #include <iostream> #include <string> #include <vector> #include "KeyValue.h" #include "Parser.h" using namespace std; class Mapper { public: Mapper(ifstream& infile); ~Mapper(void); void loadTokens(); void showTokens(); void map(); void printMap(); void printMap(string map_fileName); private: ifstream inFile; //<-- is where the error is happening vector<string>

Why am I getting this ifstream error?

烈酒焚心 提交于 2019-12-20 16:13:35
问题 Implicit instantiation of undefined template 'std::basic_ifstream<char,std::char_traits<char>>' #ifndef MAPPER_H #define MAPPER_H #include <iostream> #include <string> #include <vector> #include "KeyValue.h" #include "Parser.h" using namespace std; class Mapper { public: Mapper(ifstream& infile); ~Mapper(void); void loadTokens(); void showTokens(); void map(); void printMap(); void printMap(string map_fileName); private: ifstream inFile; //<-- is where the error is happening vector<string>

Why am I getting this ifstream error?

旧巷老猫 提交于 2019-12-20 16:13:27
问题 Implicit instantiation of undefined template 'std::basic_ifstream<char,std::char_traits<char>>' #ifndef MAPPER_H #define MAPPER_H #include <iostream> #include <string> #include <vector> #include "KeyValue.h" #include "Parser.h" using namespace std; class Mapper { public: Mapper(ifstream& infile); ~Mapper(void); void loadTokens(); void showTokens(); void map(); void printMap(); void printMap(string map_fileName); private: ifstream inFile; //<-- is where the error is happening vector<string>