ah yes its working. I modify my class so it check f.eof after I read the 
file(with if else condition) instead as the while condition. I wonder why? 
Logically isnt it the same? I mean isnt if(!f.eof()) equal to while(!f.eof())?

Thanks to every1. Really helpfull :)


--- 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, 4:56 PM
> Dear Jos,
> 
> Copy, paste and enjoy reading your executables.
> 
> As a bonus, there's a byte counter.
> 
> See that where ++u is located it also adds the eof byte (it
> also 
> belongs to the file, doesn't it?).
> 
> Wanting to disregard the eof byte place ++uu here: if
> (!f.eof()) 
> {cout << byte; ++u;} else break;
> 
> Alternatively, leave ++u where it is and subtract 1 later.
> 
> # include <fstream>
> # include <iostream>
> 
> using namespace std;
> 
> int main() {//1
> 
> char s[256];
> cout << "File: ";
> cin.getline(s, 256);
> ifstream f(s, ios::binary);
> 
> if (!f.is_open()) {cout << "Error..."
> << '\n'; return -1;}
> 
> char byte;
> unsigned u=0;
> 
> while (1) {//2
> f >> byte;
> ++u;
> if (!f.eof()) cout << byte; else break;
> }//2
> 
> cout << "\nBytes: " << u;
> 
> f.close();
> 
> }//1
> 
> 
> --- In [email protected], Jos Timanta Tarigan 
> <[EMAIL PROTECTED]> wrote:
> >
> > 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 
> > > <jos_t_tarigan@> 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]
> >


      

Reply via email to