Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey,

I am having problem with variables, while debugging variable is automatically assigned to some random large value again and again after each line, Help me out of this plz

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

char* Line_Address(string path,int Delimer,int line)
{
	char* keys;
	char* temp;
	temp=new char[70];
	temp[69]='\0';
	bool Loopbrk=false;
	int iCounter=0,iDelimer=0,i=0,count=0;
	ifstream myfile(path);
	char keystroke;
	while(myfile.good())
	{
		myfile.get(keystroke);
		if(keystroke=='!')
			iCounter++;
		while(iCounter==line)
		{
			myfile.get(keystroke);
			
			if(keystroke=='$')
				iDelimer++;
			while(iDelimer==Delimer)
			{
				myfile.get(keystroke);
				
				temp[i]=keystroke;
				i++;
				if(keystroke=='$')
				{
					Loopbrk=true;
					break;
				}
			}
			if(Loopbrk==true)
				break;
		}
		if(Loopbrk==true)
				break;
	}
	int size=strlen(temp);
	for(int i=0;i<size;i++)
		if((temp[i]>=48&&temp[i]<=57)||(temp[i]>=97&&temp[i]<=122)||(temp[i]>=65&&temp[i]<=90)||temp[i]==32)
			count++;	
	keys=new char[count+1];
	for(int i=0;i<count;i++)
		keys[i]=temp[i];
	keys[count]='\0';
	delete []temp;
	return keys;
}
int T_Delimer(string path)
{
	int iDelimer=0;
	char keystroke=' ';
	ifstream myfile(path);
	while(myfile.good())
	{
		while(keystroke!='!')
		{
			myfile.get(keystroke);
			if(keystroke=='$')
				iDelimer++;
		}
		break;
	}
	return iDelimer;
}
void main()
{
	string keystroke[10];
	int jpg=0,gif=1,Delimers=T_Delimer("student.txt");;
	char key;
	for(int i=0;i<Delimers;i++)
	{
		keystroke[i]=Line_Address("student.txt",i,gif);
	}
	fstream myfile("student.txt");
	while(myfile.good())
	{
		myfile.get(key);
		if(key=='!')
			jpg++;
		if(jpg==gif)
		{
			fstream("student.txt",ios::in);
			myfile<<" ";
		}
	}
	myfile.close();
}


////////////////student.txt//////////////

l114443$Talha Jamil$samsung$12$17$2004$UnderGraduate$!
l114445$Kamran Javed$jannat123$12$12$2009$UnderGraduate$!
l114093$Imran Khan$pluto123$11$4$2009$UnderGraduate$!
l114444$Jamil Ahmed$origin123$11$12$2009$UnderGraduate$!
l114606$Zain Malik$samsung$9$16$2009$Graduate$!
l114441$Saad Kamran$samsung$11$4$2004$Graduate$!
Posted
Comments
[no name] 13-Dec-12 9:11am    
you are required to initialize each variable before using it for any purpose. declaring it only means a particular memory bunch is allotted and the value there will not be altered until requested.

any question regarding this is welcome :)
saad_lah 13-Dec-12 9:19am    
could you plz show it in code
nv3 13-Dec-12 9:57am    
Haven't you asked that question yesterday with regard to some parameter of your function? It seems you are having problems with the debugger. Which platform are you on and which compiler and debugger do you use?
saad_lah 13-Dec-12 10:16am    
I am using visual studio 2010, though the problem was with debugger, now i managed to solve it

The effects you are seeing are typical for buffer overflows.

You have two arrays with fixed sizes that are accessed without bound checking:


  1. temp used in Line_Address() is accessed by the variable i. If i is greater or equal to 70, you will have a buffer overflow.
  2. keystroke[10] in main() is accessed up to the index Delimers - 1. There will be a buffer overflow if Delimers is greater than 10.

To fix these, you must add code that checks the indexes and terminates the loops if the limits are reached.

You are calling strlen(temp) without terminating temp. You initially terminated it, but this will be overwritten when i becomes 69. Even when i is less than 69, you are processing garbage because strlen() may return any value between the number of assigned chars and 69. To fix this, add this line after the while loop:
C++
temp[i] = 0;


You are also copying count items from temp to keys. This will not generate a buffer overflow because you are only redaing from temp, but you may have garbage in keys when i < count.

There might be more errors that I have missed.
 
Share this answer
 
v2
Comments
saad_lah 13-Dec-12 10:15am    
No, problem was with visual studio even after cleaning or rebuilding project nothing happened, but after making a new project and compiling the code in it, the program runs fine ... well thnx for the reply
Jochen Arndt 13-Dec-12 11:13am    
It might be that your program does now what you expect. But this does not mean that my notes are wrong. The possible buffer overflows and copying of garbage data are still there and may come into effect later (e.g. when passing other data). If this is some kind of homework, your teacher may note this too. You should think about my comments to avoid such errors in the future.

If you have specific questions about them, you are welcome to ask for clarification.
Pascal-78 13-Dec-12 12:03pm    
I agree. He should try using student.txt files with some format error:
- Big string between 2 '$' signs (more than 70)
- more than 10 '$' signs on a line
- use file with CRLF endline and one other file with CR only
- characters after '!' in the same line
...
saad_lah 13-Dec-12 12:22pm    
@Jochen Arndt:: I never said that your notes was wrong, that was my fault or of debugger which i managed to solve. In fact i would like to thank you for your help :)
saad_lah 13-Dec-12 13:03pm    
@Pascal :: yup i changed the temp size from 70 to 100 considering my needs in the program
You should initialize the full content of temp to zero and not only the last character:
C++
memset(temp,0,sizeof(temp));

or the strlen(temp) will be wrong.
 
Share this answer
 
Comments
saad_lah 13-Dec-12 10:16am    
temp working fine
Problem is solved by making a new project and compiling this code in it.
 
Share this answer
 
Comments
Pascal-78 13-Dec-12 11:04am    
Now with your compiled program:
did you try to look at the "temp" variable content before the strlen(temp) call?
Did you try to call the Line_Address function with these parameters: ("student.txt", 0, 0) And did you get the expected string?
And in the keystroke[0] in main(), did you see the expected value?
saad_lah 13-Dec-12 12:59pm    
Sorry for the late reply,
Line_Address("student.txt",0,0) the func. fails, in order to make the func. efficient i used a check of Line and Delimer to be '1' otherwise func. gives error. and in student.txt first line is '\n' and the student data starts from 2nd line with '$' as first character....actual student.txt is given below

/////////////student.txt////////////

$l114443$Talha Jamil$samsung$12$17$2004$UnderGraduate$!
$l114445$Kamran Javed$jannat123$12$12$2009$UnderGraduate$!
$l114093$Imran Khan$pluto123$11$4$2009$UnderGraduate$!
$l114444$Jamil Ahmed$origin123$11$12$2009$UnderGraduate$!
$l114606$Zain Malik$samsung$9$16$2009$Graduate$!
$l114441$Saad Kamran$samsung$11$4$2004$Graduate$!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900