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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading Array with elements containing spaces

Status
Not open for further replies.

debih99

Programmer
Mar 22, 2001
7
US
I had written a pgm, loading numeric data into an array. It worked great. Then, I tried to load data from a file, in which one of the elements has a space and now I'm having a problem. It seems like once the space is encountered, the rest of the data is not loaded. The element with the space is called CompanyName.
I have included the header file and some of the pgm code.

The Data File:
1234567TestBid123ABC COMPANY+000400.00+000800.00
7654321TestBid456DEF COMPANY+000500.00+000700.00

The Header File:
#define SVC_NUMBER 7
#define BID_NAME 10
#define COMPANY_NAME 11
#define GROSS_ACT 10
#define GROSS_PLAN 10


The reason why I created this structure, is in reality, the file is much bigger than what I am showing here. I don't want to copy each element separtely.

/*-- Driver Data --*/
typedef struct
{
char CompanyName [COMPANY_NAME];
char GrossAct [GROSS_ACT];
char GrossPlan [GROSS_PLAN];
}
tDRIVERDATA;

/*-- Driver Key Data --*/
typedef struct
{
char SvcNum [SVC_NUMBER];
char BidName [BID_NAME];
tDRIVERDATA tDriverData;
}
tDRIVERKEY;


Program Code:
#include "driver.h"
#define ARRAY_DATA 31
MAXLINE 5000
MAXCOLS 3

/*-- File Pointers --*/
FILE *fpIN1;
char *psIN1;
tDRIVERKEY tInFile1;


/*-- Array --*/
struct tARRAY
{
char KeySvcNumber[SVC_NUMBER+1];
char KeyBidName[BID_NAME+1];
char KeyData[ARRAY_DATA+1];
};

struct tARRAY driverKEY[MAXLINE][MAXCOLS];

int main(void);
{

psIN1 = getenv("DRIVER");
if ((fpIN1=fopen((char *)psIN1,"r")) == NULL)
{
printf("Can't open DRIVER for reading.\n");
exit(1);
}


while ( fgets(buf1,MAXLINE, fpIN1) !=NULL)
{

/*-- Parse each element into buf1 area --*/
/*-- Then strcpy into array and include a string terminator '\0'--*/

sscanf(buf1,"%7s%10s%31s",tInFile1.SvcNum, tInFile1.BidName,&tInFile1.tDriverData);

strcpy(driverKEY[array_row][0].KeySvcNumber,tInFile1.SvcNum);
driverKEY[array_row][0].KeySvcNumber[SVC_NUMBER] = '\0';

strcpy(driverKEY[array_row][1].KeyBidName,tInFile1.BidName);
driverKEY[array_row][1].KeyBidName[BID_NAME] = '\0';

strcpy(driverKEY[array_row][2].KeyData,&tInFile1.tDriverData);
driverKEY[array_row][2].KeyData[ARRAY_DATA] = '\0';

++array_row;

}

close file.......

}
 
reading looks good and I don't think that buf1 does not include Company name.
I think problem is in strcpy(). You better first set zero terminator at your string in temporary structure and then copy it to array of structures. (or use strncpy at least).
As I know strcpy will look for this terminator in input string and if it is not there .... will copy heck of a chunk of memory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top