Facing problem with getline function!!!
Facing problem in comparing strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
string s="Hello";
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
strcmp(s,line); // Error line
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
|
I know strcmp is not there in std:: lib . But i want to compare these two string how i can compare them??
Hi Kalki , convert string to char array ( but char array is const char * so convert const char * to char *)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// Example program
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
string s = "test";
char *c = "test";
char *ch = (char*)s.c_str();
cout << strcmp(c,ch) << endl;
return 0;
}
|
Hi, use the
==
operator
Don't go backwards by casting to
char *
Topic archived. No new replies allowed.