|
Search
|
||||||
C++ ifstream pointerHTML & Coding HTML, XHTML, CSS, XML and other coding issues you may code into. |
![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
C++ ifstream pointer
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?? #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; } |
| Advertisement |
|
#2
|
|||
|
|||
|
Oops, thats not the code, forgot to put it back so it makes sense: the countline() function's references to the pointer have been changed.
#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; } |
|
#3
|
||||
|
||||
|
Quote:
Using an ifstream object instead of an ifstream pointer seems clearer to me. C++ really doesn't like it when you redefine constants for it - such as NULL. Hope that helps.
__________________
Posting Guidelines Last edited by Core; Dec 11th, 03 at 5:28 PM. |
![]() |
| Tags |
| ifstream, pointer |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Mouse pointer went crazy! | Ken R. | General Hardware | 4 | Mar 14th, 04 9:24 PM |