Passing a ifstream to a function

This is a discussion on Passing a ifstream to a function within the C++ Programming forums, part of the General Programming Boards category; I'm making a class for image filters. The data for the class will be read from a text file. I ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Passing a ifstream to a function

    I'm making a class for image filters. The data for the class will be read from a text file. I want to get lines of text from the file, and to do so I am trying to make a GetLine() function for it to save a lot of repetitive code. Since trying to do this I have suddenly got a long list of errors, and I think they are all because I am doing something wron with the way I am passing the filestream into the function. This is possible right?

    Heres my code in full, anyone know how I can fix it? The relevant parts should be in the Load(), and GetLine() functions. Cheers.
    Code:
    #include <fstream>
    
    typedef unsigned char  Uint8;
    typedef unsigned short Uint16;
    typedef unsigned int   Uint32;
    
    const int FILTER_NAME_MAX_LINE = 256;
    enum FilterLoad { FILTER_LOAD_SUCCESS, FILTER_DATA_FILE_NOT_FOUND, FILTER_NAME_TOO_LONG, FILTER_DIMENSIONS_MUST_BE_ODD };
    
    
    class Filter
    {
        public:
            Filter();
            ~Filter(){};
            int Load(const char*, Uint16);
            char* GetName();
            //void Apply(Image);
        private:
            int GetLine(ifstream, char*);
            char *name;
            Uint8  w, h;
            double factor;
            double bias;
            double *grid;      
    };
    
    Filter::Filter()
    {
        name=NULL;
        w=0; h=0;
        factor=0.0;
        bias=0.0;
        grid=NULL;
    }
    
    int Filter::Load(const char* filename, Uint16 entry_num)
    {
        std::ifstream in(filename);
        if(!in.is_open()) 
            return FILTER_DATA_FILE_NOT_FOUND;
    
        //Variables
        char tmp[FILTER_NAME_MAX_LINE]; 
        int len;                     
        char c;
        char *pos;
    
        //Load the filter name
        len=GetLine(in, tmp);
        name = new char[len];
        strcpy(name, tmp);   
    
        return FILTER_LOAD_SUCCESS;
    }
    
    char* Filter::GetName()
    {
        return name;
    }
    
    int Filter::GetLine(ifstream in, char *dst)
    {
        char src;
        int i;
        for(i=0; src != '\n'; i++)
        {
            if(i >= FILTER_NAME_MAX_LINE) return -1;
            in.get(src);
            *dst++=src;
        }
        *dst='\0';
        return i;
    }
    
    int main()
    {
        Filter test;
        switch(test.Load("test.txt", 0))
        {
            case FILTER_DATA_FILE_NOT_FOUND:  
                printf("File not found.");
                break;
            case FILTER_NAME_TOO_LONG:
                printf("Name too long.");
                break;
            case FILTER_LOAD_SUCCESS:
                printf("%s", test.GetName());
                break;
        }
        getchar();
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can't copy streams like that, and as such, they can't be passed by value. Pass a reference to ifstream instead.
    My best code is written with the delete key.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I thought I tried that but I coulent seem to get it to work. As in my prototype:
    Code:
    int GetLine(ifstream*, char*);
    And function:
    Code:
    int Filter::GetLine(ifstream *in, char *dst)
    But I keep getting errors. I also tried calling the function both like:
    Code:
    len=GetLine(in, tmp);
    And;
    Code:
    i=GetLine(&in, tmp);
    But neither worked

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you need to use a REFERENCE,

    Code:
    int GetLine(ifstream &, char*);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Okay, I'm a bit confused here. Guess I'll have to learn this stuff better. I declaring my prototype as:
    Code:
    int GetLine(ifstream &, char*);
    Calling my function like:
    Code:
    len=GetLine(&in, tmp);
    Then having the function like:
    Code:
    int Filter::GetLine(ifstream *in, char *dst)
    But im still getting loads of error messages. The first of which is on the line where I declare my prototype saying: expected `;' before '(' token . Which it has been saying all along and dosent seem to make much sense now, if that bits right.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >int Filter::GetLine(ifstream *in, char *dst)
    Reference, not pointer:
    Code:
    int Filter::GetLine(ifstream& in, char *dst)
    then:
    Code:
    len=GetLine(in, tmp);
    My best code is written with the delete key.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Thanks. I don't really know exactly what a reference is yet, but I'll find out. That should work now, but it seems something else is also causing problems.

    I'm still getting the expected `;' before '(' token on the line where I declare my prototype and I have no idea why. Also my compiler is telling me that in my Load function GetLine is undeclared when it blatantly is. Any idea whats causing this?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That second error is probably because of the first one.

    Maybe it should be std::ifstream where you are typing ifstream?

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah, that fixed it. I usually have a using namespace std; at the top of my code, but dident do it this time and forgot to add that bit. Cheers dude

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Post the line (and two before & after perhaps) where it's complaining about semicolon before ( or whatever it was.

    2. Complaining about function not existing is usually caused by different types, e.g. something taking an int and a char, being passed an int * and a char would not be the same function. In this case, I think it's the fact that you need std::ifstream, not ifstream.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 08:24 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Passing a function to a function
    By lend0g in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2003, 09:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21