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

Recommended Answers

All 12 Replies

There are 3 getline() calls in your example, which one is causing the problem? Could you make a simple piece of code that shows the problem and doesn't have a lot of extra stuff in it? That way it might be easier for people to help you out.

thanks for the reply
but there are only two getline() calls the first one is causing the problem I tried to implement a simple version of the same code and it works fine but when i start to compare for this problem the first getline() only reads the first line of the file and then the code exits..

Just because ravenous miscounted does not make his suggestion wrong. Do as recommended.

OK here is the simple code from which comparison has been omitted and the place where these comparisons will be done has been marked and whe i debug it still sah the same problem

#include <iostream>
#include<fstream>
#include<sstream>
#include<istream>
#include <string>
using namespace std;
string line;
int main()
{
	char item1[160], item2[160];//, status[12];
	char *ptr1, *ptr2;//, *add1, *add2;
	int match=0, linenum=0, anomalycount=0;
	for(int i=0; i<160; i++)
	{
		item1[i]='\0';
		item2[i]='\0';
	//	status[i]='\0';
	}
	int dummy=0;

	ifstream in1("file1.txt");
	if(!in1)
	{
		cout<<"\n cannot open file";
		return 1;	
	}
	
//	cout<<"\n printing File 2\n";
	ifstream in2("sample3.txt");
	if(!in2)
	{
		cout<<"\n cannot open file";
		return 1;	
	}

	if(in1.is_open())
		while (in1.eof()!=1)
		{
			
			{
				dummy++;
				in1.getline(item1,160);//, '\n');//,'#');//,'\n');
				ptr1=item1;
				//		if(in1) cout<<item1<<endl;
			}
			if(in2.is_open())
				while (!in2.eof())
				{
					{
						linenum++;
						in2.getline(item2,160);//,'\');
						ptr2=item2;
					}
					ptr1=item1;	// As the 1st element of line is taken as 0 for unknown reasons so a space is given at the start
					ptr2=item2;	// so incrementinf the ptr by 1 points at the start => duration of connection

					/* COMPARISON CODE WILL COME HERE*/

					
					if (match==1)
					{		
						cout<<"\nAnomaly Detected on line number ="<<linenum;
						anomalycount++;
					}

				}while (in2.eof()!=1);
		}while (in1.eof()!=1)
	in2.close();
	in1.close();


	cout<<"\nTotal anomalies detected ="<<anomalycount<<endl;
	cout<<"\n The program ends here";
	cin>>dummy;
	_flushall();
	return 0;
}

This code has lots of small errors that I'm going to leave it to you to find. I'll just point out the big error.

Lines 47-66 read all the contents of in2. When you're done, you've reached the end of in2. Now line 67 tests whether you've reached the end of in1.

Suppose you didn't reach the end of in1 yet. So now you go back to line 38, and eventually reach line 47 which checks whether you've reached the end of in2. But of course you did; otherwise you wouldn't have fallen out of the loop that ends on line 66.

So for each line you read from in1 beyond the first, you will never read anything new from in2 because you've read it all already.

As I said at the beginning, there are many other problems with this code. However, you won't even get to the point where you might have a chance at finding them unless you fix this big problem first.

Thank You arkoenig for pointing out the big mistake. after correcting that and some of the smaller one i have my code working now. I am now opening the second file within the loop of the first file in this way every time one line is read from the first file second line is opened and the whole file is searched for the line from file 1.
all of this comes in the /*File search section*/ as indicated in the code.

Can you point out one or two of the smaller mistakes that I am making Although VS2008 shows me 0 errors and 0 warning. I would be more thank ful

Just because ravenous miscounted does not make his suggestion wrong. Do as recommended.

Actually, there are three, but one is commented out ;o)

Yes Now i can see the third one ;P

Can you point out one or two of the smaller mistakes that I am making Although VS2008 shows me 0 errors and 0 warning. I would be more thank ful

For example, the statement

while (in1.eof()!=1)

is wrong because the last time through the loop, your attempt to read a line will fail (because you had not reached eof before you started to read the line); but your code does nothing to detect the failure.

The use of in1.eof()!=1 instead of simply in1.eof() also suggests that you do not understand how C++ handles Boolean values.

As a more general comment, you should not be using char arrays for a program such as this one, because it guarantees problems if your files have lines in them that are longer than the number of elements in your arrays.

You define a string variable named temp, but never use it.

You define a label named skip, but never use it.

You call a function _flushall that is not part of standard C++.

Your comment "As the first element of line is taken as zero for unknown reasons..." strongly suggests that there are important aspects of your program that you do not understand.

I'm sure there are others. I will be happy to look more carefully at the program once I see evidence that you have looked at it carefully.

Actually, there are three, but one is commented out ;o)

:icon_rolleyes: Then the commented out getline() must be the culprit...

For example, the statement

while (in1.eof()!=1)

should be used as while(!in.eof())
should I use here a do while loop to ensure that first I read line and then i check for eof

As a more general comment, you should not be using char arrays for a program such as this one, because it guarantees problems if your files have lines in them that are longer than the number of elements in your arrays.

You define a string variable named temp, but never use it.

You define a label named skip, but never use it.

You call a function _flushall that is not part of standard C++.

this problem =>"As the first element of line is taken as zero for unknown reasons..." strongly suggests that there are important aspects of your program that you do not understand. is resolved now and it was due to one of the errors because the file was from some other PC when i made the file my self it is working fine

I'm not sure I understand how do you know it was another pc. What would another pc have to to with it? C++ is C++ and shouldn't change from pc to pc. Just because it seems to work doesn't mean it's correct. Many errors can hide in bad code. Remember the pentium problem with floating point numbers from years ago? Look it up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.