Search code examples
includeidentifierseekg

"seekg identifier not found"


I have a program called main:

#include<iostream>
#include<fstream>
using namespace std;
#include"other.h"
int main()
{
//do stuff
}

and then other.h:

char* load_data(int begin_point,int num_characters)
{
    seekg(begin_point);
    char* return_val=new char[num_characters+1];
    mapdata.getline(return_val,num_characters);
    return return_val;
}

and I get the error:

'seekg': identifier not found

why do I get this error and how do I fix it?


Solution

  • seekg is a method from the fstream (declared in istream) class.

    You haven't instantiated any.

    Take this as an example

      ifstream is;
      is.open ("test.txt", ios::binary );
    
      // get length of file:
      is.seekg (0, ios::end);
    

    source: http://www.cplusplus.com/reference/iostream/istream/seekg/

    So, you should

    char* load_data(int begin_point,int num_characters)
    {
        ifstream is;
        is("yourfile.txt") //file is now open for reading. 
    
        seekg(begin_point);
        char* return_val=new char[num_characters+1];
        mapdata.getline(return_val,num_characters);
        return return_val;
    }
    

    Take into account what ParoXon commented in your question.

    You should create a file other.cpp containing function's load_data implementation. File other.h should contain function's load_data declaration. In that file (other.h) you should include all files neccesary for functions declared there to work. And dont forget to protect yourself against multiple includes !

    File other.h

    #ifndef __OTHER_H__
    #define  __OTHER_H__
    
    #include <iostream>
    #include <fstream>
    
    char* load_data(int,int);//no implementation
    #endif
    

    File other.cpp

    #include "other.h" //assumes other.h and other.cpp in same directory
    
    char* load_data(int begin,int amount){
          //load_data implementation
    }