Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   C++ ifstream pointer question (https://thetfp.com/tfp/tilted-technology/38767-c-ifstream-pointer-question.html)

Prince 12-11-2003 06:02 AM

C++ ifstream pointer question
 
Here's the code, i just want to count the lines of a file. I get a bunch of "invalid simple type name destructor" errors..
It makes a pointer to an input stream to the file specified by the command line parameter. Then it passes the pointer to countlines() to try to count the lines. anyone know why it doesn't work? :|

Quote:

#include <fstream.h>

#define NULL 0
#define WIDTH 257

int countlines(ifstream *in);

int main(int numArgs, char *args[])
{
if (numArgs != 2)
{
cout << "Usage:\n reader filename.ext";
return 1;
}
ifstream *fin;
fin = new ifstream(args[1], ios::in | ios::nocreate);
cout << countlines(fin);
//char buf[WIDTH];

//for(int i=0; !*fin.eof(); i++)
//{
// *fin.getline(buf, WIDTH);
// cout << buf << "\n";
//}
//
*fin.close();
return 0;
}

int countlines(ifstream *in)
{
int lines = 0;
char null[WIDTH];
while (!*in.eof())
{
*in.getline(null, WIDTH);
lines++;
}
*in.seekg(0, ios::beg);
delete null;
return lines;
}


Prince 12-11-2003 06:03 AM

Well, vB removes some small parts of the code apparently in its battle against html tags, but I hope that is understandable anyway.

Pragma 12-11-2003 08:06 AM

Quote:

#include iostream
#include fstream
using namespace std;

static const int width = 257;

int countlines(ifstream &in);

int main(int numArgs, char *args[]) {
if (numArgs != 2) {
cout << "Usage:\n reader filename.ext";
return 1;
}
ifstream fin(args[1]);
cout << countlines(fin);

fin.close();
return 0;
}

int countlines(ifstream &in) {
int lines = 0;
char null[width];
while (!in.eof()){
in.getline(null, width);
lines++;
}
in.seekg(0, ios::beg);

return lines;
}

Pragma 12-11-2003 08:11 AM

Well, you can't do "delete null;", as it's not dynamic memory.

Other than that, why are you using an ifstream pointer instead of just an ifstream object?

Furthermore, C++ really doesn't like it when you redefine constants for it - such as NULL.

Prince 12-11-2003 08:27 AM

You're a genius. Thanks! :)

cheerios 12-11-2003 12:12 PM

for future reference code tags (use em just like quote ones) are great :)

charliex 12-11-2003 08:32 PM

its ok to redefine #define constants, you can even use #undef if you want too first.

NULL is compiler specific, so sometimes you want to override it because the compiler maker goofed it up , say using (void*)0 instead of 0 or 0L

Pragma 12-12-2003 06:00 AM

True, you can redefine constants, but most compilers will give you warnings, if not errors, if you don't #undef before you #define.

Either way, I've seen very few cases (and this not being one of them) were actually redefining NULL is warranted.

charliex 12-12-2003 05:57 PM

You should never get an error redefining a #define, a typedef yes, if you do the compilers broken.

some compilers will generate an error using #undef on something that was never #define'd so you should always guard it.

Unfortunately theres no easy way of telling the difference between a typedef and a #define.

You might get an error on the use of the redefined #define, but thats not because of the redefine itself.

I like to redefine NULL on projects i know are going crossplatform, then i know what its going to be, theres only a few rare systems where NULL isn't 0, but i've seen enough uses of (void*) 0 to do it, plus it doesn't hurt anything.

using null as an arrayname though is asking for trouble

dnd 12-12-2003 06:22 PM

arghhhh i forgot how much i hated c++ .... i'm so glad i learnt java first....wrong way round i know...but its just so much easier! pointers may be helpful, but understanding them takes a long time!

charliex 12-12-2003 06:32 PM

yep a lot of people probably can get by without pointers, but the things i miss the most in java are pointers, enums and #defines (though i do use #defines and run it through a preprocessor )

Oh and a stable development environment, and speed .. ok'll i'll stop :)

oberon 12-13-2003 11:10 AM

Pointers are pretty important to figure out. Can't be a good programmer without understanding them, in my opinion.

Pragma 12-13-2003 02:49 PM

If you think C/C++ pointers are bad, go learn Brainfuck. You'll petition to have the word "pointer" removed from the English language.

oberon 12-14-2003 05:57 AM

LOL, yeah, brainfuck is a great language for that purpose. :D

Next, try to figure out assembly (any architecture, preferably x86 for maximum pain) without understanding pointers. :D


All times are GMT -8. The time now is 04:25 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37