Hey guys, I am trying to take chars from a file, and place them into 2 different dynamically allocated arrays.

An example of the file would be

name:John Doe&id:1448&phone:98765

One array stores name, id, and phone, and another should store each value.

I have gone ahead and taken the string and converted it into a char array, past that, I am at a loss for how to take the chars and use strcpy to get them into the arrays. Thanks for your help!

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
        string fileName, line;
        fstream accountInfo;
		char *entireString = new char[0];
        char **keys= new char*[0];
		char **values = new char*[0];

        cout << "Please enter the name of the file you wish to use." <<endl;
        cin >> fileName;
		accountInfo.open(fileName.c_str());
        if(!accountInfo)
        {
                cout << "Unable to open file!";
                exit(1);
        }
		
        while(getline(accountInfo,line))
        {
				int count = 0;
				int columnCount = 0;
				entireString[line.length()+1];
				strcpy(entireString,line.c_str());
				for(int i = 0; i < line.length(); i++)
				{
					if(entireString[i] == ':')
					{
						for(int i = entireString.length() + 1; i < count ; i++)
						{
						
						}
					
						count = 0;
						columnCount++;
					}
					if(entireString[i] == '&')
					{
						count = 0;
					}
					if(entireString[i] == ':')
					{
						count = 0;
					}
					if(entireString[i] == '&')
					{
						count = 0;
					}
					if(entireString[i] == ':')
					{
						count = 0;
					}
					if(entireString[i] == '&')
					{
						count = 0;
					}
					if(entireString[i] != ':' || entireString[i] != '&')
					{
						count++;
					}
				}
				
		}
		accountInfo.close();
}

The if statements were an idea of mine to gradually progress through the array, find out where I am in the array, and strcpy the values behind it somehow into the array, thus the counters.

And I realize my statement in the for loop is wrong. Working on that problem too :)

Updated code:

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
        string fileName, line;
        fstream accountInfo;
		char *entireString = new char[0];
        char **keys= new char*[0];
		char **values = new char*[0];
		
        cout << "Please enter the name of the file you wish to use." <<endl;
        cin >> fileName;
		accountInfo.open(fileName.c_str());
        if(!accountInfo)
        {
                cout << "Unable to open file!";
                exit(1);
        }
		
        while(getline(accountInfo,line))
        {
				int count = 0;
				int columnCount = 0;
				entireString[line.length()+1];
				strcpy(entireString,line.c_str());
				cout << line << endl;
				for(int i = 0; i < line.length(); i++)
				{
					if(entireString[i] == ':')
					{
						cout << "FOUND1!" << endl;
						for(int j = 0; j < i ; j++)
						{
							
							keys*[j] = entireString*[j];
						}
						count = 0;
						i++;
						columnCount++;
						
					}
					if(entireString[i] == '&')
					{
						cout << "FOUND2!" << endl;
						for(int j = 0; j < i ; j++)
						{
							
							values*[j] = entireString*[j];
						}
						i++;
						count = 0;
					}
					if(entireString[i] != ':' || entireString[i] != '&')
					{
						cout << "SPACE" << endl;
						count++;
					}
				}
				
		}
		accountInfo.close();
}

Still am trying to figure out how to copy from the char array to my dynamically allocated arrays. If anyone could point me to a tutorial, I would appreciate it :)

If I understand what you want to do correctly, you want to do something like an associative array or a map where the first array simply indicates what type of thing is in the second array?
So: if (keys[ n ] == "name") cout << "Name = " << values[ n ] << endl; You need to read up on dynamic memory allocation and fix your syntax. Lines 14 and 15 each ask to allocate zero bytes of memory (an array of zero elements is zero elements long...).
Also, your syntax is screwy. Neither line makes any sense.

What errors are you getting from the compiler (the first few ought to be enough).


Frankly, since this is C++, you should be using a map<string, string> instead of an array of array of char.

Does this make sense?

Haha, we can only use pointers, not even allowed to use vectors to make it easy ^_-. I will look into what you said, thanks!

We are suppose to dynamically allocate memory to the array as we read in the file, so I initially set it to 0. It is suppose to get larger as I feed it in more data.

I think what my prof wants is more like


keys // values
name 0 John Doe
id 1 2223322
phone 2 998885554


keys is a 2d dynamic array and values is a 2d dynamic array

I see... You're going to have to ask your professor for clarification on how he wants you to allocate to the array. There is no reasonably simple way to resize a dynamically allocated array in C++. A linked list would work...

Alas.


He might be looking for something along the lines of char *keys[ 50 ]; Where each key is a dynamically allocated array of char. But there is no way to dynamically allocate only part of the keys array at a time --it's all or nothing.

You don't need c_strings. Just look for the two characters that begin and end a value you want moved and use the substr() method to move the value.

And why are you indenting 3 miles? Too much indenting is almost as bad as no indenting. 4 spaces is enough for any indent.

Hm, what if I just set the size of each array to be the size of the input file? I guess that would be the closest I could get...


Formatting got messed up somehow, I will fix it next time if it occurs again

That would probably be total overkill, but it would work...
Also, WaltP makes a very good point. Since this is a C++ program, would your professor totally object to using std::strings along with the rest of the STL?

Ok, is this what you were thinking?

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
        string fileName, line;
        fstream accountInfo;
		char *entireString = new char[0];
        char **keys= new char*[0];
		char **values = new char*[0];
		
        cout << "Please enter the name of the file you wish to use." <<endl;
        cin >> fileName;
		accountInfo.open(fileName.c_str());
        if(!accountInfo)
        {
                cout << "Unable to open file!";
                exit(1);
        }
		
        while(getline(accountInfo,line))
        {
		cout << line << endl;
		int tempPosition1 = 0;
		int tempPosition2 = 0;
		int tempLength = 0;
		entireString[line.length()+1];
		keys[line.length()+1];
		values[line.length()+1];
		strcpy(entireString,line.c_str());
		for(int i = 0; i < line.length(); i++)
		{
			if(entireString[i] == ':')
			{
				tempPosition2 = i+1;
				tempLength = tempPosition2 - tempPosition1;
				cout << line.substr(tempPosition1,tempLength-1) << endl;
				tempPosition1 = i+1;
			}
			if(entireString[i] == '&')
			{
				tempPosition2 = i+1;
				tempLength = tempPosition2 - tempPosition1;
				cout << line.substr(tempPosition1,tempLength-1) << endl;
				tempPosition1 = i+1;
			}
		}
		tempLength = tempPosition2 - tempPosition1;
		cout << line.substr(tempPosition1,tempLength-1) << endl;	
	}
	accountInfo.close();
	delete entireString;
	delete values;
	delete keys;
}

Now I just need to figure out how to sort the list alphabetically, that's the next step :)


EDIT: Formatting is still off.... @_@ XCODE is NOT playing nice

Ok, is this what you were thinking?

No. Get rid of the char* and just use string . It'll make the code simpler. The problems I see so far:

char *entireString = new char[0];    // this defines a char string with no characters

entireString[line.length()+1];    // what are you expecting these statements to do?
keys[line.length()+1];
values[line.length()+1];

strcpy(entireString,line.c_str());  // don't need this at all, plus you just screwed up your memory

Wouldn't deleting the strcpy statement destroy how I found : and & 's?

Size in the [] is because of this paragraph in my lab packet


Your arrays of pointers should never have more than 4 pointers that are not in use. This means that you
cannot start your program and allocate and array of 500 pointers and hope for the best ☺. You will have to
implement functionality that will allow you to resize your arrays of pointers as your file is parsed.

I don't understand how I can resize my array of pointers as it is parsed yet.

Updated code:

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
        string fileName, line;
        fstream accountInfo;
		char *entireString = new char[0];
        char **keys= new char*[0];
		char **values = new char*[0];
		
        cout << "Please enter the name of the file you wish to use." <<endl;
        cin >> fileName;
		accountInfo.open(fileName.c_str());
        if(!accountInfo)
        {
                cout << "Unable to open file!";
                exit(1);
        }
		
        while(getline(accountInfo,line))
        {
			cout << line << endl;
			int tempPosition1 = 0;
			int tempPosition2 = 0;
			int tempLength = 0;
			entireString[line.length()];
			keys[line.length()];
			values[line.length()];
			strcpy(entireString,line.c_str());
			for(int i = 0; i < line.length(); i++)
			{
				if(entireString[i] == ':')
				{
					tempPosition2 = i+1;
					tempLength = tempPosition2 - tempPosition1;
					cout << line.substr(tempPosition1,tempLength-1) << endl;
					tempPosition1 = i+1;
				}
				if(entireString[i] == '&')
				{
					tempPosition2 = i+1;
					tempLength = tempPosition2 - tempPosition1;
					cout << line.substr(tempPosition1,tempLength-1) << endl;
					tempPosition1 = i+1;
				}
			}
			tempLength = tempPosition2 - tempPosition1;
			cout << line.substr(tempPosition1,tempLength-1) << endl;	
		}
		accountInfo.close();
		
		int temp;
		
		for(int i = line.size() - 1 ; i >= 0 ; i-- )
		{
			for( int j = 1; j <= i ; j++)
			{
				if(entireString[j-1] > entireString[j])
				{
					temp = entireString[j-1];
					entireString[j-1] = entireString[j];
					entireString[j] = temp;
				}
			}
		}
		for(int i = 0; i < line.size() ; i++)
		{
			for(int j = 0 ; i < line.size() ; i++)
			{
				cout << keys[i][j] << endl;
				cout << values[i][j] << endl;
			}
		}		
		delete entireString;
		delete values;
		delete keys;
}

My sort is to put the names in alphabetical order. It appears that it is instead sorting every char instead of each name. Is it because its a character array and cant compare strings?
ie I should use strcmp somehow?

EDIT: The formatting is getting worse >.<

Alright, I am having trouble storing the substrings I have created into a 2D char array. I am getting error message that I cannot convert 'std::basic_string<char,std::char_traits<char>, std::allocator<char> > ' to 'std::string*' in initialization

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
        string fileName, line;
        fstream accountInfo;
		char *entireString; 
        char **keys;
		char **values;
		
        cout << "Please enter the name of the file you wish to use." <<endl;
        cin >> fileName;
		accountInfo.open(fileName.c_str());
        if(!accountInfo)
        {
                cout << "Unable to open file!";
                exit(1);
        }
		
        while(getline(accountInfo,line))
        {
			cout << line << endl;
			int tempPosition1 = 0;
			int tempPosition2 = 0;
			int tempLength = 0;
			entireString[line.length()];
			keys[line.length()];
			values[line.length()];
			strcpy(entireString,line.c_str());
			for(int i = 0; i < line.length(); i++)
			{
				if(entireString[i] == ':')
				{
					tempPosition2 = i+1;
					tempLength = tempPosition2 - tempPosition1;
					keys = new char*[tempLength];
					strcpy(keys,line.substr(tempPosition1,tempLength-1));
					tempPosition1 = i+1;
				}
				if(entireString[i] == '&')
				{
					tempPosition2 = i+1;
					tempLength = tempPosition2 - tempPosition1;
					values = new char*[tempLength];
					strcpy(values, line.substr(tempPosition1,tempLength-1));
					tempPosition1 = i+1;
				}
			}
			tempLength = tempPosition2 - tempPosition1;
			values = new char*[tempLength];	
			strcpy(values,line.substr(tempPosition1,tempLength-1));
		}
		accountInfo.close();
		
		int temp;
		
//		for(int i = line.size() - 1 ; i >= 0 ; i-- )
//		{
//			for( int j = 1; j <= i ; j++)
//			{
//				if(entireString[j-1] > entireString[j])
//				{
//					temp = entireString[j-1];
//					entireString[j-1] = entireString[j];
//					entireString[j] = temp;
//				}
//			}
//		}
//		for(int i = 0; i < line.size() ; i++)
//		{
//			for(int j = 0 ; i < line.size() ; i++)
//			{
//				cout << keys[i][j] << endl;
//				cout << values[i][j] << endl;
//			}
//		}		
		delete entireString;
		delete values;
		delete keys;
}

EDIT: Tried everything, but pasting from XCODE is destroying formatting.

No, you haven't tried everything. You haven't tried one suggestion nor answered one question I posted. Can't help you if you don't care enough to explain your thinking or think about suggestions given.

Sorry, I was referring to the extra spacing in the code, the formatting is being messed up when I paste it into the code tags.

I did try implement the substr suggestion, I will try to remove the c string references.

This question has already been answered. Start a new discussion instead.