fstream

Reading and writing a vector of classes

安稳与你 提交于 2020-01-05 08:43:50
问题 I have an inheritance hierarchy with three/four levels. And within each level will hold at least one or more classes with different attributes that make that object of a class unique (and of course, inheriting attributes from the level above). Each object of a class may have different attributes to another therefore my question is, how can I read and write each object to a file and differentiate the attributes? I do apologise if I have not worded this very well but will be very appreciative

C++之 fstream open函数( error: no matching function for call to ‘std::basic_ifstream::open(const)

狂风中的少年 提交于 2020-01-04 02:44:43
C++之 fstream open函数( error: no matching function for call to ‘std::basic_ifstream::open(const) C++使用fstream进行文件读写,非常的方便,但是在日常使用的时候,常常会忽视掉一些小问题,如下: 类定义为: 出现的问题的open函数: 报错为: 但是看上去确实没什么问题,看官方文档: 可以看出,fstream::open有两种重载方式: open( const char* filename, ios_base::openmode mode=ios_base::in | ios_base::out);(可以看出这种写法明显是为了兼容C的写法。); open( const string& filename, ios_base::openmode = ios_base::in | ios_base::out); 可以看到open函数是打开由参数filename标识的文件,将其与流对象相关联,以便对其内容执行输入/输出操作。参数mode指定文件打开的模式。 所以大概可判断是filename形式出现了问题。 接着看open函数的参数: 可以看到,上面出问题的地方就在于filename;即filename是要打开的文件名的字符串。 其格式和有效性的细节取决于库的实现和运行环境。 所以

How to take input file, reverse word order, write to output file in C++

允我心安 提交于 2020-01-03 06:35:50
问题 Take the contents of an input text file and write them in reverse order of words to an output file. Your program can ignore line breaks. You will need to use arrays. (unnecessary crap removed) Aaaaah, panic, please help! EDIT: What I have so far. Still lost on how to reverse word order now. //Kristen Korz //CIS 22A //This program reads an input file and writes the words in reverse order to an output file. #include <iostream> #include <fstream> using namespace std; int main() { //create and

Reading multiple data types on one line using C++

可紊 提交于 2020-01-03 06:30:07
问题 I am attempting to pull from a dat file a date composed of ints as well as a char and a float value that are together. Dat file format looks like: 201205171200 M29.65 201207041900 F30.3 And so on. I am struggling with separating these values. Here is what I have so far: #include <iostream> #include <fstream> #include <vector> using namespace std; int main() { int inCount = 0; //This variable will be used to keep track of what record is being read. vector<int> dates; vector<float> temps; //

Replace line in txt file c++

早过忘川 提交于 2020-01-03 05:05:35
问题 I just wondering cause i have a text file containing STATUS:USERID:PASSWORD in accounts.txt example it would look like this: OPEN:bob:askmehere: OPEN:john:askmethere: LOCK:rob:robmypurse: i have a user input in my main as such user can login 3x else status will change from OPEN to LOCK example after 3 tries of john before: OPEN:bob:askmehere: OPEN:john:askmethere: LOCK:rob:robmypurse: after: OPEN:bob:askmehere: LOCK:john:askmethere: LOCK:rob:robmypurse: what i have done is: void lockUser

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

∥☆過路亽.° 提交于 2020-01-02 01:56:30
问题 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?

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

六月ゝ 毕业季﹏ 提交于 2020-01-02 01:56:06
问题 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?

Difference between casting ifstream to bool and using ifstream::is_open()

此生再无相见时 提交于 2020-01-01 10:04:13
问题 Maybe a dummy question, but I need a clear answer to it. Is there any difference at all in the return of any of those functions int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return !!file; } int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return file.is_open(); } So in other words, my question is: does casting the fstream to bool give exactly the same result as fstream::is_open()? 回答1: No. is_open checks only whether there is an

Why I can't read from file using “file_ptr>>variable” in my program?

柔情痞子 提交于 2019-12-31 07:07:19
问题 In the following program I am trying to understand how to read and write files. #include<iostream> #include<fstream> using namespace std; int main() { fstream myfile; string str1; myfile.open("H:/input_file.txt"); if(myfile.is_open()) { myfile<<"test1 writing files"<<" "; myfile>>str1; cout<<str1<<endl; } return 0; } Why I don't get any output on the console even though "test1 writing files" is written into a file? 回答1: The file will need to be opened for both read and write (I'm sorry,

How do I ensure data is written to disk before closing fstream?

为君一笑 提交于 2019-12-31 02:42:52
问题 The following looks sensible, but I've heard that the data could theoretically still be in a buffer rather than on the disk, even after the close() call. #include <fstream> int main() { ofstream fsi("test.txt"); fsi << "Hello World"; fsi.flush(); fsi.close(); return 0; } 回答1: You cannot to this with standard tools and have to rely on OS facilities. For POSIX fsync should be what you need. As there is no way to a get C file descriptor from a standard stream you would have to resort to C