Board index » delphi » Array of char arrays

Array of char arrays

I am trying to read a file with data (date, sales ID, amount) like:

12/27/1999 156-67-9000 450.78
12/29/1999 210-78-6578 953.67
01/13/2001 987-56-4531 211.89

and put the 3 fields into arrays but the dates and the soc sec #s won't go
into my arrays why?

Here is my code:

#include <fstream.h>

const int datemax=11;
const int socsecmax=12;
const int salesmax=7;
const int max = 3;

int main()
 char transdate[max][datemax], TempTransDate[datemax];
 char salesid[max][socsecmax], TempSalesID[socsecmax];
 float salesamt[max], TempSalesAmt;

 ifstream infile("sales.dat");

 for(int i=0;i<max;i++)

  transdate[i][datemax]=TempTransDate[datemax]);  //here is the problem
  salesid[i][socsecmax]=TempSalesID[socsecmax];   //here is the problem
return 0;





Re:Array of char arrays

On Sat, 24 Feb 2001 15:13:41 -0500, "Neo" <>

>char transdate[max][datemax], TempTransDate[datemax];
>char salesid[max][socsecmax], TempSalesID[socsecmax];
>transdate[i][datemax]=TempTransDate[datemax]);  //here is the problem
>salesid[i][socsecmax]=TempSalesID[socsecmax];   //here is the problem

You can't copy C-style arrays by using the assignment operator (=).
You can only do that with C++ string classes which have overloaded
the = operator. Use strcpy() or strncpy() to copy C-style arrays. e.g. -
strcpy(transdate[i], TempTransDate);
strncpy(transdate[i], TempTransDate, datemax);

You might benefit from reading ptrtutor.txt at

Wayne A. King

Re:Array of char arrays

On Sun, 25 Feb 2001 00:55:18 GMT, (Wayne A. King) wrote:

>Use strcpy() or strncpy() to copy C-style arrays.

That should read: " ... to copy C-style strings." (nul-terminated char arrays)
To copy C-stlye arrays which do *not* hold C-style strings, use memcpy()
or memmove().

Wayne A. King

Other Threads