ifstream

I'd like to use ifstream and ofstream in C++ to mimic C#'s BinaryReader / BinaryWriter functionality

女生的网名这么多〃 提交于 2020-01-05 04:34:18
问题 I'm looking for a way to write floats/ints/strings to a file and read them as floats/ints/strings. (basically read/write as ios::binary). 回答1: I subclassed ifstream and ofstream : ibfstream and obfstream . I made a little helper class that would detect the endianness of the machine I was compiling/running on. Then I added a flag for ibfstream and obfstream that indicated whether bytes in primitive types should be flipped. These classes also had methods to read/write primitive types and arrays

problem opening file c++

陌路散爱 提交于 2020-01-04 23:41:32
问题 Must be a simple answer but I am at a loss, here is the code that is returning an error. I have tried with and without the starting slash. I won't know the full path, I want it to be relative from the exe, and that is the relative path. I tried escaping the slashes. My problem is that i get "error opening file" when the file is there. why is it failing? ifstream myFile("/LOGS/ex090716.txt"); if (myFile.fail()) {cout << "Error opening file";} else { cout << "File opened... \n"; //string line;

Why does istream_iterator<string>(ifstream(“test.txt”)) cause an error?

喜你入骨 提交于 2020-01-03 17:08:23
问题 I have tried to write a code to read strings from file named "test.txt" and write the strings to standard output. The code below works well: int main() { using namespace std; ifstream file("test.txt"); copy(istream_iterator<string>(file), istream_iterator<string>(), ostream_iterator<string>(cout, " ")); } However, with this modification, the code no longer compiles: int main() { using namespace std; copy(istream_iterator<string>(ifstream("test.txt")), // <-- Error here istream_iterator<string

Creating fstream object from a FILE* pointer

南笙酒味 提交于 2020-01-01 23:54:13
问题 The well known way of creating an fstream object is: ifstream fobj("myfile.txt"); ie. using a filename. But I want to create an ifstream object using a file descriptor. Reason: I want to execute a command using _popen() . _popen() returns the output as a FILE* . So there is a FILE* pointer involved but no filename. 回答1: You cannot do that just in standard C++, since iostreams and C I/O are entirely separate and unrelated. You could however write your own iostream that's backed by a C FILE

Creating fstream object from a FILE* pointer

回眸只為那壹抹淺笑 提交于 2020-01-01 23:53:38
问题 The well known way of creating an fstream object is: ifstream fobj("myfile.txt"); ie. using a filename. But I want to create an ifstream object using a file descriptor. Reason: I want to execute a command using _popen() . _popen() returns the output as a FILE* . So there is a FILE* pointer involved but no filename. 回答1: You cannot do that just in standard C++, since iostreams and C I/O are entirely separate and unrelated. You could however write your own iostream that's backed by a C FILE

How to convert a string to a ifstream

萝らか妹 提交于 2020-01-01 14:27:19
问题 i am trying to open a file with ifstream and i want to use a string as the path (my program makes a string path). it will compile but it stays blank. string path = NameOfTheFile; // it would be something close to "c:\file\textfile.txt" string line; ifstream myfile (path); // will compile but wont do anything. // ifstream myfile ("c:\\file\\textfile.txt"); // This works but i can't change it if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } } I am

How to convert a string to a ifstream

馋奶兔 提交于 2020-01-01 14:26:35
问题 i am trying to open a file with ifstream and i want to use a string as the path (my program makes a string path). it will compile but it stays blank. string path = NameOfTheFile; // it would be something close to "c:\file\textfile.txt" string line; ifstream myfile (path); // will compile but wont do anything. // ifstream myfile ("c:\\file\\textfile.txt"); // This works but i can't change it if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } } I am

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

ifstream, end of line and move to next line?

穿精又带淫゛_ 提交于 2020-01-01 02:41:22
问题 how do i detect and move to the next line using std::ifstream? void readData(ifstream& in) { string sz; getline(in, sz); cout << sz <<endl; int v; for(int i=0; in.good(); i++) { in >> v; if (in.good()) cout << v << " "; } in.seekg(0, ios::beg); sz.clear(); getline(in, sz); cout << sz <<endl; //no longer reads } I know good would tell me if an error happened but the stream no longer works once that happens. How can i check to see if i am at the end of line before reading another int? 回答1: Use

c++读写文件流

自作多情 提交于 2019-12-30 09:45:51
掌握文本文件读写的方法 了解二进制文件的读写方法 C++文件流: fstream  // 文件流 ifstream  // 输入文件流 ofstream  // 输出文件流 //创建一个文本文件并写入信息 //同向屏幕上输出信息一样将信息输出至文件 #include<iomanip.h> #include<fstream.h> void main() {   ofstream f1("d:\\me.txt");           //打开文件用于写,若文件不存在就创建它   if(!f1)return;                 //打开文件失败则结束运行   f1<<setw(20)<<"姓名:"<<"廉东方"<<endl;     //使用插入运算符写文件内容   f1<<setw(20)<<"家庭地址:"<<"河南郑州"<<endl;   f1.close();                   //关闭文件 } 运行后打开文件d:\me.txt,其内容如下:        姓名:廉东方      家庭地址:河南郑州 文件操作: 打开文件   文件名     注意路径名中的斜杠要双写,如:     "D:\\MyFiles\\ReadMe.txt"   文件打开方式选项:     ios::in    = 0x01, //供读,文件不存在则创建