I dont think im able to convert my class to use f >> byte. Maybe u should see the method i use. Its in the attachment. Somehow I get twice the last value '5'. Any got idea?
thanks in advance ps: why cant i send a file with main.cpp attached? --- On Thu, 10/9/08, py2akv <[EMAIL PROTECTED]> wrote: > From: py2akv <[EMAIL PROTECTED]> > Subject: [c-prog] Re: Loop to Read Binary File > To: [email protected] > Date: Thursday, October 9, 2008, 10:59 AM > char byte; > ifstream f("file"); > > while (1) { > f >> byte; > if (!f.eof()) cout << byte << '\n'; > else break; > } > > Mind the break to escape the while on eof found: no > additional > iteration. > > The idea is here; modify at will! > > Geraldo > > > --- In [email protected], Jos Timanta Tarigan > <[EMAIL PROTECTED]> wrote: > > > > Hi, > > Im currently try to read a binary file. Im using this > loop > > > > while(!f.eof() {do.something()} > > > > to do the loop. I notice that this method is nor > recommended since > there will be an additional loop at the end. How can I > loop a binary > file without using this loop? What is the other way to do > it? > > > > Thank you > > > > > > Regards > > > > Jos Timanta Tarigan > > ---------- #include <iostream> #include <fstream> #include <vector> using namespace std; void writeToFile(ofstream &f, vector<int> &v) { int type = 0; cout<<"Printing vector" << endl; for(int i = 0; i<v.size(); i++) { type = v.at(i); f.write(reinterpret_cast<const char *>(&type), sizeof(int)); cout << "Write type: " << type << endl; } } void readFromFile(ifstream &f, vector<int> &v) { int type; int i = 0; while(f) { f.read(reinterpret_cast<char *> (&type), sizeof(int)); cout << "Type:" << type<< endl; cout << i++ << endl; } } int main() { vector<int> v; v.push_back(4); v.push_back(5); v.push_back(1); v.push_back(8); v.push_back(5); ofstream file; file.open("testV.txt"); writeToFile(file, v); cout << "FIle written"<<endl; file.close(); ifstream inFile; inFile.open("testV.txt"); readFromFile(inFile, v); inFile.close(); return 0; } [Non-text portions of this message have been removed]
