Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to input from file to two-dim. array

Status
Not open for further replies.

Bloosy

Technical User
Jun 23, 2003
15
0
0
Hi everybody. I want to put 4 strings from a file into a two-dimensional array. This is how I declared it:
Code:
char* suppliers[10][4];
I intend to have a file wich stores 10 suppliers, all having 4 attributes: Name, Address, Contact and Telefone.
I have edited the file suppliers.txt, each supplier in one row, attributes separated with space. I have then tried this:
Code:
int main() {
	ifstream infile;
    	
	infile.open("suppliers.txt");
	if(!infile) {cout<<"File couldn't be opened"; exit(1);}
	int i = 0;
	infile>>suppliers[i][0]>>suppliers[i][1]>>suppliers[i][2]>>suppliers[i][3];
	
	infile.close();

just to test one row at least. I have included all necessary headers, program compiles and links ok, but fails to run with a message "...Wholesale.exe has encountered a problem and needs to close. We are sorry for the inconvenience". Could anyone tell me what's wrong
Thanks
 
You declared a 2D array of char* pointers, but you didn't allocate any char arrays for those pointers before you read the data into the array.

You said the 4 attributes in your file are separated by spaces... I don't see how that can work though since a person's name has a space "john smith", and addresses have spaces "123 main st." Maybe you should use a different delimitor like '|'.

I'd also use a different data structure, such as:
Code:
struct Supplier
{
   string  Name;
   string  Address;
   string  Contact;
   string  Phone;
};

vector<Supplier>  suppliers;
That way you don't have to worry about allocating/deallocating memory, and the array can grow dynamically as much as needed.
 
Yes but we haven't learnd vectors yet. I have seen the example in two different books for this thing:
infile>>string1>>string2>>string3...
and it would put words from file separated by space respectively into strings. In my case instead of strings, I have:
infile>>suppliers[0][0]>>suppliers[0][1]>>...
suppliers[][] are char pointers, which means strings, and should work the same. Or am I in mistake here?
Thanks for reply.
 
No, a string is an object created from a class. That class handles all the work of allocating and deallocating memory for the string.

Have you learned how to use new and delete yet?
A char* pointer can't hold any data because it only has 4 bytes to store an address to a block of memory where the actual string can go.

If you know the maximum length of each of the strings in the file, you could do something like this:
Code:
const size_t MAX_LENGTH = 1024;  // or whatever size you think is appropriate.
char buf[ MAX_LENGTH ];

infile >> buf;
suppliers[0][0] = new char[ strlen( buf ) + 1 ];
strcpy( suppliers[0][0], buf );
// Repeat for [0][1], [0][2], [0][3]...
Then when you're done with those strings, don't forget to delete them:
Code:
delete [] suppliers[0][0];
delete [] suppliers[0][1];
...
 
Thank you for qick response. No, we haven't learned new delete either. But this is how I think: every char pointer we can assign a string, like this
char *string2 = "Hello";
so because I declared an array of char pointers, I should be able to assign a string to each, ie
suppliers[0][0] = "Tesco";
suppliers[0][1]= "Matthew";
suppliers[0][2]= "AbbeyRoad";

etc.
I have found in this book a lot of examples whith strings without actually using a String object, they only do with char pointers. That's what I want to achieve now.
 
You can assign your char* pointers like that if you use literal strings in quotes, but the >> operator of ifstream is expecting you to already have a block of char's already allocated to your char* pointer.
Since your char* pointers aren't even initialized to any particular value, they could be pointing anywhere in memory and as soon as you try to access that memory which doesn't belong to you, your program crashes.

Maybe you should use a 3D array until you learn about new/delete or STL strings.
Ex. char suppliers[100][10][4];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top