ifstream

Reading/writing files to/from a struct/class

限于喜欢 提交于 2019-12-01 13:01:52
I'd like to read a file into a struct or class, but after some reading i've gathered that its not a good idea to do something like: int MyClass::loadFile( const char *filePath ) { ifstream file ( filePath, ios::in | ios::binary ); file.read ((char*)this, 18); file.close(); return 0; } I'm guessing if i want to write a file from a struct/class this isn't kosher either: void MyClass::writeFile( string fileName ) { ofstream file( fileName, ofstream::binary ); file.write((char*)this, 18); file.close(); } It sounds like the reason i don't want to do this is because even if the data members of my

How to read the 6th character from the end of the file - ifstream?

早过忘川 提交于 2019-12-01 12:51:33
问题 void maintainFileName () { std :: ifstream myfile; myfile.open ("zoomLevels.txt"); if (myfile.is_open ()) { // Move to end of the file, myfile.seekg (0, std::ios::end); // and then six characters back to pick up the last file number. myfile.seekg (6, std::ios::beg); int len = 1; char *t = new char[len]; myfile.read(t, len); qDebug () << "\nt: " << *t << "\n"; } else { qDebug () << "\nsorry"; } } The file contains this: 78.8115,29.582,1,01.rda 78.8115,29.582,2,02.rda 76.3671,30.2201,1,11.rda

Reading/writing files to/from a struct/class

风格不统一 提交于 2019-12-01 11:15:24
问题 I'd like to read a file into a struct or class, but after some reading i've gathered that its not a good idea to do something like: int MyClass::loadFile( const char *filePath ) { ifstream file ( filePath, ios::in | ios::binary ); file.read ((char*)this, 18); file.close(); return 0; } I'm guessing if i want to write a file from a struct/class this isn't kosher either: void MyClass::writeFile( string fileName ) { ofstream file( fileName, ofstream::binary ); file.write((char*)this, 18); file

C++ ifstream from linux to arduino

吃可爱长大的小学妹 提交于 2019-12-01 11:08:08
original code #include<iostream> #include<fstream> using namespace std; int main() { ofstream arduino_output("/dev/ttyACM0"); ifstream arduino_input("/dev/ttyACM0"); int value; string txt; while(cin >> value){ arduino_output << value << endl; arduino_input >> txt;//I never recieve the "OK" (Which I should get) cout << txt; } arduino_input.close(); arduino_output.close(); return(0); } Here is the problem: cin >> value; arduino_output << value << endl; arduino_input >> txt;//I never recieve the "OK" (Which I should get) cout << txt; but if I do this instead it works: cin >> value; arduino_output

Can't declare ifstream class member in header file

时光怂恿深爱的人放手 提交于 2019-12-01 10:57:43
I am trying to declare an ifstream object in a header file as is shown but I get an error saying that it cannot be accessed. I have tried various things such as making it into a pointer instead, initialising in the .c file etc. but my code can't seem to get part the declaration of it. ReadFile.h: #ifndef READFILE_H #define READFILE_H #include "cv.h" #include "highgui.h" #include <iostream> #include <fstream> class ReadFile{ private: std::ifstream stream; public: std::string read(); ReadFile(); // Default constructor ~ReadFile(); // Destructor }; #endif ReadFile.c: #include "ReadFile.h"

C++ std::ifstream in constructor problem

别说谁变了你拦得住时间么 提交于 2019-12-01 07:35:22
I've got a problem with this code: #include <fstream> struct A { A(std::ifstream input) { //some actions } }; int main() { std::ifstream input("somefile.xxx"); while (input.good()) { A(input); } return 0; } G++ outputs me this: $ g++ file.cpp file.cpp: In function `int main()': file.cpp:17: error: no matching function for call to `A::A()' file.cpp:4: note: candidates are: A::A(const A&) file.cpp:6: note: A::A(std::ifstream) After changing it to this it compile (but that is not solving the problem): #include <fstream> struct A { A(int a) { //some actions } }; int main() { std::ifstream input(

C++ type of argument to ifstream::open()

一个人想着一个人 提交于 2019-12-01 05:35:02
问题 What type must I make my file name to use it as an argument to ifstream.open() ? int main(int argc, char *argv[]) { string x,y,file; string file = argv[1]; ifstream in; in.open(file); in >> x; in >> y; ... With this code, I get the following error: main.cpp|20|error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'| gcc\mingw32\4.4.1\include\c++\fstream|525|note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char

Read/Write to PPM Image File C++

不羁的心 提交于 2019-12-01 05:15:35
问题 Trying to read and write to/from a PPM Image file (.ppm) in the only way I know how: std::istream& operator >>(std::istream &inputStream, PPMObject &other) { inputStream.seekg(0, ios::end); int size = inputStream.tellg(); inputStream.seekg(0, ios::beg); other.m_Ptr = new char[size]; while (inputStream >> other.m_Ptr >> other.width >> other.height >> other.maxColVal) { other.magicNum = (string) other.m_Ptr; } return inputStream; } My values correspond to the actual file. So I cheerfully

ifstream::read doesn't tell how many bytes it really reads?

半世苍凉 提交于 2019-12-01 03:57:15
I'm using ifstream::read to read a file, ifstream ifs("a.txt"); char buf[1024]; ifs.read(buf, 1024); But a.txt's size might be less than 1000 bytes , so how am I supposed to know how many bytes have been read from ifs ? You can get the amount of characters extracted by the last operation with std::ifstream::gcount : ifstream ifs("a.txt"); char buf[1024]; ifs.read(buf, 1024); size_t extracted = ifs.gcount(); or ifstream ifs("a.txt"); char buf[1024]; size_t extracted = ifs.read(buf, 1024).gcount(); since read(...) returns *this . 来源: https://stackoverflow.com/questions/11720880/ifstreamread

Process same file in two threads using ifstream

非 Y 不嫁゛ 提交于 2019-12-01 03:10:25
问题 I have an input file in my application that contains a vast amount of information. Reading over it sequentially, and at only a single file offset at a time is not sufficient for my application's usage. Ideally, I'd like to have two threads, that have separate and distinct ifstream s reading from two unique file offsets of the same file. I can't just start one ifstream up, and then make a copy of it using its copy constructor (since its uncopyable). So, how do I handle this? Immediately I can