fstream

Returning ifstream in a function

风格不统一 提交于 2019-12-05 19:25:58
问题 Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I need to create that file for the user. I know how to do that, but only by asking the user to restart the program after creating the file. I wanted to avoid that inconvenience for the user if possible, but the function below does not compile in gcc:

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

倖福魔咒の 提交于 2019-12-05 17:21: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: ifstream.open Holes; // ? I don't know what the proper syntax is and I can't find straightforward

C++: reading from a file with null characters

谁说胖子不能爱 提交于 2019-12-05 14:04:43
I have to read data from a file for an assignment unfortunately instead of spaces separating the various fields there are null characters. When taking integers from the file they are extracted fine however with the strings i just get a blanks space and garbage from my uninitialized character array. Any ideas as how to just extract the characters into my character array ignoring the null characters. EDIT: char fName[15],lName[15],pMethod[5],roomType[10],purpose[15]; int days, roomNum; long guestID; datafile>>guestID; datafile.getline(fName,15,'\0'); datafile.getline(lName,15,'\0'); cout<

How to read a file line by line to a string type variable?

孤街醉人 提交于 2019-12-05 09:22:53
I'm trying to read a file line by line to a string type variable using the following code: #include <iostream> #include <fstream> ifstream file(file_name); if (!file) { cout << "unable to open file"; exit(1); } string line; while (!file.eof()) { file.getline(line,256); cout<<line; } file.close(); it won't compile when I try to use String class, only when I use char file[256] instead. how can I get line by line into a string class? Use std::getline : std::string s; while (std::getline(file, s)) { // ... } 来源: https://stackoverflow.com/questions/2581761/how-to-read-a-file-line-by-line-to-a

What can go wrong if cout.rdbuf() is used to switch buffer and never set it back?

旧巷老猫 提交于 2019-12-05 08:13:29
The author presented this code under the title A bus error on my platform #include <fstream> #include <iostream> int main() { std::ofstream log("oops.log"); std::cout.rdbuf(log.rdbuf()); std::cout << "Oops!\n"; return 0; } The string "Oops!\n" is printed to the file "oops.log". The code doesn't restore cout's streambuf, but VS2010 didn't report a runtime error. Since log and std::cout share a buffer, that buffer will probably be freed twice (once when log goes out of scope, then once more when the program terminates). This results in undefined behavior, so it's hard to tell the exact reason

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

本小妞迷上赌 提交于 2019-12-05 04:45:37
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 the hideous cast and find a way that is in accordance with the C++ standard. Update: see also Is it safe

Why would I ever open a file (std::ifstream) without std::ios::binary?

≡放荡痞女 提交于 2019-12-05 03:30:29
This may properly belong to a different part of Stack Exchange but I don't think so - programmers.se is more about other things. Getting to the question: There are things you can do with std::ios::binary that you cannot do in text mode (E.g. relative seek) but I cannot find anything to do in text mode that you cannot do in binary mode - even reading the file as text with e.g. std::getline() So why would I ever open as text? As a perhaps-related question, why not open as binary by default? Whose use-case does that break? EDIT Additional information Here's what's causing me to ask: I have a file

Why is my fstream being implicitly deleted?

大兔子大兔子 提交于 2019-12-05 01:29:16
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 and write to file response = send_command(READ_DEVICE); file.write((char *)&response[0], response.size())

fstream to const char *

微笑、不失礼 提交于 2019-12-04 17:37:30
What I want to do is read a file called "test.txt", and then have the contents of the file be a type const char *. How would one do this? #include <string> #include <fstream> int main() { std::string line,text; std::ifstream in("test.txt"); while(std::getline(in, line)) { text += line + "\n"; } const char* data = text.c_str(); } Be careful not to explicitly call delete on data It's highly unlikely you really want to do that. The contents of the file (which may be either text, or binary data) are unlikely to represent a (valid) pointer to a char on your architecture, so it is not really

How to protect log from application crash?

末鹿安然 提交于 2019-12-04 14:19:42
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 couple of lines and using append - but that still doesn't protect me from the situation when the program