View Full Version : ifstream rewind!
elsheikhmh
hi all,
can anyone help me in this simple C++ program? actually it does nothing rather than printing the contents of a text file: msg.txt. but the great problem that i don't know how to rewind the get pointer, i.e. when i use printAll() for the second time, it get nothing.
i think the key of this program is some kind of REWIND.
thank you, i appreciate you time!
mustafa
---
// <<<<<< program starts here >>>>>>
#include <fstream.h>
// minbox is a simple derived class form ifstream
class minbox : public ifstream
{
public:
// print all the contents of the file
void printAll(void)
{
char* line=new char[80];
// rewind. see BUG comment in main()
this->seekg(NULL);
while(this->getline(line,81))
cout << line << endl;
}
};
void main(void)
{
// msg.txt contains any text..
minbox inbox; inbox.open("msg.txt");
inbox.printAll();
cout << "-------\n";
// BUG HERE. nothing is printed this time!
inbox.printAll();
inbox.close();
}
// <<<<<< EOF >>>>>>
alaa
first of all your C++ code shows that you're learning from a very old and faulty source your code should look like this.
// <<<<<< program starts here >>>>>>
#include <fstream> // #1
#include <iostream>
using namespace std;
// minbox is a simple derived class form ifstream
class minbox : public ifstream
{
public:
// print all the contents of the file
void printAll() //#2
{
char line[80]; //#3
// rewind. see BUG comment in main()
clear();
seekg(0,ios::start); //#4
while(!eof()) { //#5
getline(line,80) //#6
cout << line << endl;
}
};
int main() //#7
{
// msg.txt contains any text..
minbox inbox;
inbox.open("msg.txt");
inbox.printAll();
cout << "-------\n";
// BUG HERE. nothing is printed this time!
inbox.printAll();
inbox.close();
}
// <<<<<< EOF >>>>>>
will tell you whats wrong with your code in the next post
MaherG
Why do use inheritance?
Use inheritance only when you are going to add functionality or rewrite certain functions.
Maher
alaa
ok I'll use the numbered comments to refer to code portions.
#1 standrard header files ending with .h are depreacated you should include C++ header file without the .h, and C headers files with a c preceding them.
example
#include <iostream>
#include <cstdlib>
when you include a header without .h its contents are placed in the namespace std, if you don't know namespaces you should learn them, meanwhile you can just add
using namespace std;
to your programs to make them act like the old .h headers.
#2 you don't need to declare functions without arguments as foo(void) in C++ empty brackets after a function name means the function takes no arguments (in C it means you give the compiler no info about the function arguments thats why C programmers write foo(void)).
#3 you should not use dynamically allocated memory unless the size is undetermined, and if you have to you should always deallocate the memory you acquired, otherwise you'll have memory leaks.
since you always allocate 80 chars it makes sense to declare line staticaly and let the compiler do the memory management.
#4 several problems here, first you called functions this way this->foo() I don't know where you learned this (PHP maybe) but in C++ member functions can access members directly, so use foo() instead of this->foo()
as for seekg() you wrote seekg(NULL) while this translates to seekg(0) and will work it is very bad style, you should only use NULL with pointers, I prefer to use the more verbose version of seekg where you tell it an offset and a starting point instead of just stating an absolute position.
as for the reason why your code didnot work, the first time you run printall() the stream hits eof, and sets a special eof flag to true, the flag remains in this state until you clear it, all calls to seekg are ignored, in fact since you code tried to read beyond the eof seekg will be stuck at -1 which is an error value so you need to clear more than the eof flag, this is done by calling clear();
#5 your way probably works but using eof() makes your intentions more clear.
#6 again a grave memory error, you passed getline 81 instaed of 80, getline needs an extra character to put the terminating NULL character in it, thats why it reads size-1 chars, by passing 81 getline will read 80 characters and put the null character outside the boundries of the line array, which could overwrite other memory (the famous buffer overflow bug) and corrupt program memory.
#7 in C++ main has to return int, if you don't explicitly write the return statement the compiler adds return 0; to the end of your main() function, void main() is plain wrong and should not compile at all.
I think you need to get back to the basics and relearn C++, what book are you working from??
I recommend you try and get Bjarn Stroustrop's The C++ Programming Language (3rd edition), if you can afford it and P2P is not your way try the Free Thinking in C++ it is quite good.
cheers,
Alaa
alaa
Originally posted by MaherG
Why do use inheritance?
Use inheritance only when you are going to add functionality or rewrite certain functions.
Maher
good point Maher, OOP can be very abused, one should only inherit when it makes sense to do so, C++ offers many techniques and one should try and mix them.
basically a C++ program will have portions that use
Object Oriented Programming (using inheritance)
Data Abstraction (using datastructure classes and Standard containers)
Generic Programming (using templates)
procedural programming (c style stuff)
a bit of functional programming (through function objects)
cheers,
Alaa
elsheikhmh
thank you alaa. i appreciate your time! actually, your comments are useful. at first, i didn't like your reply at all:mad:. but you pushed me to write code in a good way. when i rewrote the code following your op, i felt that what i posted is very ugly code written by a VERY bad programmer! :D. thanx again.
maher, the code posted here is not the complete program, and i actually i need inheritance -or i think so.
about book, i used "C++ Ezone Interactive Course (without the interactive part)" along with "SAMS' Teach Yourself C++..". but i think they are fool books. i take a look over "The C++ Programming Language 3rd Edition (Stroustrap)" -i have it PDFed- i found it great and worthy)
i hope that you don't get annoyed.
--
mustafa
alaa
no not annoyed at all, I love C++ but it pains me to see the horible ways it is abused because of bad books and entrenched C practices.
did you know that for instance the best selling C++ books (and the ones required in nearly all our universities) are written by this guy Herb Schildt, who is considered by all the comp.std.c++, com.lang.c++, boost.org C/C++ journal, ACM, the ISO C++ commetee and the rest of the C++ community as the worst and the most inacurate author ever.
anyway, if you're taking C++ seriously here is a list of essential books.
basic language:
The C++ Programming Language 3rd edition - Bjarn Stroutrop'
Thinking in C++ 2nd edition - Bruce Eckel (first volume is not needed if you can grasp BJ's book 2nd part is still worth reading)
standard library:
The C++ Standard Library - Nicolai M. Josuttis
STL Tutorial and Reference Guide 2nd Edition - David R. Musser
Design:
Modern C++ Design - Andrei Alexandrescu (a must read, you are not a C++ programmer until you understand this book)
Designing Components with the C++ STL - Ulrich Breymann
Gory Language details:
Effective C++ - Scott Meyers
More Effective C++ - Scott Meyers
Effective STL - Scott Meyers
Exceptional C++ - Herb Sutter
More Exceptional C++ - Herb Sutter
Optimization:
Efficient C++ Programming Techniques - Dov Bulka & David Mayhew
C++ Footprint and Performancs Optimization - Rene Alexander & Graham Bensley (not very good but very few books cover the topic).
I'm yet to find a good book on Object Oriented Programming, and I still need a book that realy explains generic programming concepts not just how to use the STL and templates.
as you can see C++ is a complex language and lots of reading needs to be done, but don't worry you don't have to learn it all at once.
interesting tidbits, in g++ there is a special warning mode that warns you if your code does not follow the guidlines in Effective C++ programming :-)
g++ -Weffc++
www.boost.org is dedicated to complementing the C++ standard library with other useful features (including extra datastructures like trees, a regular expression library a typesafe any type, etc), the ISO C++ standard commitee is even considering adopting some of boost into the next standard.
Happy Coding
vBulletin v3.0.1, Copyright ©2000-2004, Jelsoft Enterprises Ltd.