C++ ifstream pointer

Discussion in 'Web Design & Coding' started by haileris23, Dec 11, 2003.

  1. haileris23

    haileris23

    Joined:
    Nov 21, 2003
    Messages:
    16
    Likes Received:
    0
    Location:
    San Antonio, TX
    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;
    }
     
    haileris23, Dec 11, 2003
    #1
    1. Advertisements

  2. haileris23

    haileris23

    Joined:
    Nov 21, 2003
    Messages:
    16
    Likes Received:
    0
    Location:
    San Antonio, TX
    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;
    }
     
    haileris23, Dec 11, 2003
    #2
    1. Advertisements

  3. haileris23

    Core in pounce mode Moderator

    Joined:
    Jun 30, 2003
    Messages:
    1,557
    Likes Received:
    24
    Location:
    Akaa, Finland
    ...you can't do "delete null;", as it's not dynamic memory.

    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.
     
    Last edited: Dec 11, 2003
    Core, Dec 11, 2003
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.