undefined reference

Hi guys,

I am trying to read the data from a flat file named data.txt.

My flat file looks like this:

USERNAME PASSWORD
USERNAME PASSWORD

I need help with the coding below. I received a debugging error " undefined reference to `read(std::basic_ifstream<char, std::char_traits<char> >&)"

I'm new to c++. Please help me. Thank you.

Here is the code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;



int main(){

//Check against the data in flat file
string read(ifstream&);
string line;
string::size_type pos;
ifstream readFile;
int count = 0;
string user_pass[50][2]; //dynamic array. 50 columns, 2 rows

//open the file data.txt
readFile.open("data.txt");

//if file cannot be found, return error message.
if(readFile == NULL) {

cout << "Invalid file. Program terminated" << endl;
return -1;
}

//As long as there are data in the flat file,
while (!readFile.eof()){

//it will read the line from the flat file
line=read(readFile);

//if the line is not empty
if (line.length() > 0) {
pos = line.find("\t",0);//find the tab delimiter

if (pos != string::npos) // npos refers to the end of string
user_pass[count][0] = line.substr(0,pos); //Get username
user_pass[count][1] = line.substr(pos+1); //Get password
count++; // iteration

}
}


}
Last edited on
Do you implement the function "read" in another source file? There is no global function with that name in the standard library. There is a method in ifstream, but it is for unformatted input and does NOT read one line at a time.
This is the only source file I have. Any advice on how to go about?
Hi Simeonz, I know what when wrong. I forgot to include readFile.close() after reading it. Thanks anyway! Cheers!
Hello guys,

It seems to be working perfectly few days ago but i got this error again today even after i close the file stream.

This time round, i declared the function in the header file and link it to my main.cpp.

Sometimes it works, sometimes not.

Any workaround?

Thank you in advance. :)

Last edited on
string read(ifstream&);

That line declares a function... it's looking for that function.
Topic archived. No new replies allowed.