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();
}