INFILE.getline(line,MAXCHARACTERS,'\n'); //search each line until you reach MAXCHARACTERS or reach a newline.
It seems to not like that line. I would check documentation on the getline function, and see exactly what params it needs.
Im doing some string searching and I keep getting this error can some one help.
################################################## #################
#include <fstream.h> //for IO
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
int main()
{
//Declare our input file name
char filename[] = "fingers.txt";
char ch_check; //checks for eof
string line;
int index; //index of where "Name: " is found
string INFO[50]; //our array for stroring the info
string stringA = "Name: ";
int i = 0; //index for number of entries in to our array
ifstream INFILE;
INFILE.open(filename);
if(INFILE.fail())
{
cout << "Couldn't open the input file: " << filename <<endl;
cout << "Please make sure that it exists and you have permissions to read it." <<endl;
exit(1);
}else{
cout << "The file " << filename << " was opened successfully" <<endl;
}
/*
while((ch_check = INFILE.peek()) != EOF)
{
INFILE.get(character); //input from our file stream
cout << character;
}
*/
//We will get the name of each person. We look for the word "Login:", this line contains the user information that we want.
int MAXCHARACTERS = 50;//the maximum number of characters to search in a line
while((ch_check == INFILE.peek()) != EOF)
{
//Search for the word "Login"
INFILE.getline(line,MAXCHARACTERS,'\n'); //search each line until you reach MAXCHARACTERS or reach a newline.
index = line.find(stringA); //look for stringA in 'line'
if(index != ios::npos) //if we find the string
{
INFO[i] = line.substr(index + 6, line.find("\n");
i += 1;
}
}
for(int j = 0; j < 50; j++)
{
cout << INFO[j];
}
INFILE.close();
return 0;
}
################################################
The error says
no matching function for calll to 'ifstream::getline (string &,int &,ch &'
Thanx,
INFILE.getline(line,MAXCHARACTERS,'\n'); //search each line until you reach MAXCHARACTERS or reach a newline.
It seems to not like that line. I would check documentation on the getline function, and see exactly what params it needs.
My book says this is the form.
(requires fstream.h header)
getline(string var, int n, '\n') - Extract characters from the input stream until either n-1 characters are read or a newline is encountered (terminates input w/ a '\0')
I declared line
string line;
ive also tried
char line[50]; //just to be sure
still no go .
I think Im decaring it wrong some how??? What's another way???
thanx
Using char[50] worked for me.
BTW: Why are you including both <string.h> and <string>? Is this program C or C++?
If it's C++ you should include the new style headers without the .h and use std:: or 'using namespace std' for short examples.
T.
(which book by the way?)
Hmm.
Im using anjuta to compile and build. could that make a differenence?
Yea I added that after getting desperate.
let me try this again....
Book doesn't always match the actual headers. Especially if you are reading a C++ book geared towards Win32.My book says this is the form.
(requires fstream.h header)
getline(string var, int n, '\n') - Extract characters from the input stream until either n-1 characters are read or a newline is encountered (terminates input w/ a '\0')
I declared line
string line;
ive also tried
char line[50]; //just to be sure
still no go .
I think Im decaring it wrong some how??? What's another way???
thanx
Bookmarks