Sign in to follow this  

Dynamic Variables/Pointers

This topic is 4689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I've been working on a program that fixes capitalization in a file, and can also do word searches. Right now it currently only can do 10 lines, but I want it to be able to look at the file, read the ammount of lines then create space to save these. The code I have is as follows, but it is incorrect... I would really appreciate the help :) Thank you for your time in advance.
bool LoadStrings(char const * szFileName, char * const * pszLines){

	fstream DataFile(szFileName, ios::in);

	if(DataFile.fail()){
		pError("LoadStrings", "Unable to locate or open file");
		DataFile.close();
		return 1;
	}

	int iTest=0;
	char szTemp[65000];
	char* szDynamic;

	while(DataFile.getline(szTemp, 65000, '\n')){
		iTest++;
	}
	
	cout << iTest;


	//for(int i=0; i<gkuiNumLines; i++){
	//	if(!DataFile.eof())
	//		DataFile.getline(*(pszLines+i), 256, '\n');
	//	else
	//		*(*(pszLines+i)) = 0;
	//}

	szDynamic = new char[iTest][gkuiLineLength];

	for(int i=0; i<iTest; i++){
		if(!DataFile.eof())
			DataFile.getline(*(szDynamic+i), gkuiLineLength, '\n');
	}

	for(int i=0; i<iTestl i++){
		cout << *(szDynamic+i);
	}
	
	delete [][] szDynamic;
	szDynamic = 0;

	cout << szDynamic;
	
	DataFile.close();
	return 0;
}


I forgot to mention, gkuilinelength is included in a headfile, its 65000.

Share this post


Link to post
Share on other sites
Could be this code wrong?


for(int i=0; i<iTest; i++){
if(!DataFile.eof())
DataFile.getline(*(szDynamic+i), gkuiLineLength, '\n');
}



perhaps you should replace:

DataFile.getline(*(szDynamic+i), gkuiLineLength, '\n');
with:
DataFile.getline(*(szDynamic+(i*64000), gkuiLineLength, '\n');

You could also check the lenght of the longer line in your first cicle (when you count the lines) and use this to se up the array lenght. And you may want to use c++ strings to store the lines (new string text[iTest]; )

Hope that i have been useful

Share this post


Link to post
Share on other sites
I assume you are asking why it is not compiling, correct? For future references, it is vital to be as specific as possible to what the problem is you are having so you can get the most help possbile.

 char* szDynamic; 


Will make a single array of characters. What you need to do instead is this

 char** szDynamic; 


Now it is an array of an array of characters, or a matrix. With that done, now you will need to allocate the memory properly.

 szDynamic = new char[iTest][gkuiLineLength]; 


Should be:

szDynamic = new char*[iTest];
for(int d=0; d < iTest; d++ )
szDynamic[d] = new char[gkuiLineLength];


Since this is multidimentional memory, you must loop through like that to allocate the memory properly. Finally you must model the delete like the create. After you delete the memory, you should NULL it and never access it again without reallocating new memory for it.
 delete [][] szDynamic; 


Should be
for( d = 0; d< iTest; d++) 
{
delete [] szDynamic;
szDynamic = 0;
}

delete [] szDynamic;
szDynamic = 0;

// BIG NO NO!
// cout << szDynamic;


Now that should at least compile. I do not know about the logics you are using yet. However, I would have to strongly say that you should stop and take a look at some tutorials before you go on. If you do not fully understand how dynamic memory works, you will be in for a world of headaches and troubles.

You can even take a look at the various STL tutorials that allow you to skip all of this low level memory allocation and get faster more efficient results. Just another suggestion though. Feel free to do what you want though, but I can tell you from experience, doing something like this without all the proper background knowledge will really be depressing when it fails to work... I've learned that lesson many times. Hope this helps. Best of luck!

- Drew

[Edited by - Drew_Benton on February 11, 2005 2:09:43 AM]

Share this post


Link to post
Share on other sites
<clippy-mode>It looks as though you are trying to allocate a multi-dimensional array. Would you like me to beat you over the head with the STL?</clippy-mode>

(For what it's worth:
- There is no such thing as "delete[][]". (See the link.)
- You are inconsistent in using the '65000' value as a literal in some places and the defined constant from your header in others.)


vector<string> LoadStrings(string filename) {
fstream DataFile(filename.c_str(), ios::in);

if(DataFile.fail()) {
DataFile.close(); // not sure we should have this
throw exception("Unable to locate or open file");
// this is what exception handling is made for. Now you get
// to actually return the logical return value of the function,
// and the stack trace will keep track of where the problem
// happened, so you don't need that info.
}

vector<string> result;
// By using a vector we can keep appending to the end, and the resizing
// will be done for us. Similarly, we don't need a static buffer to
// read into for each line; we will always have enough room (subject to
// the machine's limits on memory), yet we will not waste 64K of memory
// for lines that are really short.
// Plus, it's much easier to write:
string current;
while(getline(DataFile, current)){
result.push_back(current);
}
DataFile.close();
return result;
}


Share this post


Link to post
Share on other sites
Okay thank you guys, the post helped greatly.
Yea Im actually reading out of a C++ text book that I have boughten, but it didn't show how to do multi-dimensional ones, just the single so I was kinda trying to figure it out myself. Thank you guys for the help, I think Ill be able to get the rest to work now :)

Share this post


Link to post
Share on other sites

This topic is 4689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this