Hi every body . iam writing a code that reads a line from input file and searches it in test file. but my getline() function reads only the first line and enters an infinite loop i have read many discussions on the topic but still unable to resolve the issue. here is my code
#include <iostream>
#include<fstream>
#include<sstream>
#include<istream>
using namespace std;
std::string temp;
int main()
{
char item1[160], item2[160];//, status[12];
char *ptr1, *ptr2;//, *add1, *add2;
int match=0;
for(int i=0; i<160; i++)
{
item1[i]='\0';
item2[i]='\0';
// status[i]='\0';
}
int dummy;
ifstream in1("Input.txt");
if(!in1)
{
cout<<"\n cannot open file";
return 1;
}
// cout<<"\n printing File 1\n";
while(in1)
{
in1.getline(item1,160);//,'#');//,'\n');
//in1.get(status,12,'\n');
// if(in1) cout<<item1<<endl;
}
// cout<<"\n printing File 2\n";
ifstream in2("test.txt");
if(!in2)
{
cout<<"\n cannot open file";
return 1;
}
do//while (!in2.eof())
{
while(in2)
{
in2.get(item2,160,'\n');
//in2.getline(item2,160);//,'\');
// if(in2) cout<<item2<<endl;
}
ptr1=item1;
ptr2=item2;
cout<<"\n CHROMOSOME:\n";
for(int i=1; i<160; i++)
{
cout<<*ptr1;
ptr1=ptr1+1;
}
cout<<"\n TEST CONNECTION:\n";
for(int i=1; i<160; i++)
{
cout<<*ptr2;
ptr2=ptr2+1;
}
ptr1=item1+1; // As the 1st element of line is taken as 0 for unknown reasons so a space is given at the start
ptr2=item2+1; // so incrementinf the ptr by 1 points at the start => duration of connection
do
{
if ((*ptr1=='X'))//&&(*ptr1[i]!=',')&&(*ptr1[i]!='\0'))
{
while(*ptr1!=',')
{
ptr1=ptr1+1;
};
while(*ptr2!=',')
{
ptr2=ptr2+1;
};
}else
if((*ptr1==','))
{
ptr1=ptr1+1;
ptr2=ptr2+1;
}else
{
if ((*ptr1==*ptr2))
{
// cout<<"\n A match is found for `"<<*ptr1<<"' and `"<<*ptr2<<"'";
ptr1++;
ptr2++;
match=1;
}else
{
match=0;
break;
}
}
}while(*ptr1!='\0');
skip:
if (match==1)
{ cout<<"\nAnomaly Detected\n";}
else
cout<<"\nNothing detected\n";
in2.getline(item2,2);
}while (!in2.eof());
in1.close();
in2.close();
cout<<"\n The program ends here";
cin>>dummy;
_flushall();
return 0;
}
Any help will be highly appreciated