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

fread question

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
hey, can someone tell me why I'm printing out more than the size of the array after I fread into it. Here's what I'm doing:
char strttimestamp[12];
if (!(GetTimeStamp(strttimestamp, sizeof(strttimestamp))))

//sub
GetTimeStamp(timestamp, size)
char timestamp[12];
int size;
{
...
fread(timestamp, 1, size, strttimestamp_fp)
...
}

and this is what it prints out:
101597184028!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I've tried a number of things, and I'm losing confidence in my programming skills...

Thanks
M
 
I presume your timestamp is 12 bytes long. If you read these 12 bytes into a 12 byte area of memory there is no trailing NULL after the last of the timestamp characters. A printf will then print characters starting at the beginning of you strtimestamp area and continuing through memory from that address until it finds a NULL byte.

If you timestamp is 12 bytes long then the strtimestamp buffer should be 13 bytes long and you should set the 13th byte to NULL (strtimestamp[12]=0x00)

Cheers
 
now I can see clearly. Thanks for the extra set of eyes.

:) M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top