By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,945 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,945 IT Pros & Developers. It's quick & easy.

file io, reading in dates, problems with the 26th of every month..

P: n/a
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:
mdyn"comment~""comment~"\n

Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of comments all being 0, with the month being correct. Does anyone have any clue whatsoever as to what the problem might be? Why would the number 26 be special? I tested it and know that it is writing it to the file correctly, so the problem is in the reading. Here is some sample reading code that produces the problem:

DWORD MyOpenFile()
{
DWORD br, m, d, yr, n;
DWORD nBuffer[200];
char ch = ' ';
File * f = fopen(filename, "r");
if (f == NULL)
return 0;

//mdyn"comment~""comment~"\n
br = fread(nBuffer, sizeof(DWORD), 4, f);
m = nBuffer[0]; //so far always correct
d = nBuffer[1]; //if date was the 26th of the month, == 0
yr = nBuffer[2]; //if date was the 26th of the month, == 0
n = nBuffer[3]; //if date was the 26th of the month, == 0

fclose(f);
return 0;
}

Any help would be greatly appreciated, as I am completely dumbfounded. Thanks in advance!

-NH

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Need Helps wrote:

Now, I wrote some code to read these records, and it works perfectly
for every date I've tried it on, except when the day is 26. I tried
saving a record for 6/26/2004 and 7/26/2004 and it read it in as the
day, year, and number of comments all being 0, with the month being
correct. Does anyone have any clue whatsoever as to what the problem
might be? Why would the number 26 be special? I tested it and know that
it is writing it to the file correctly, so the problem is in the
reading. Here is some sample reading code that produces the problem:

DWORD MyOpenFile()
{
DWORD br, m, d, yr, n;
DWORD nBuffer[200];
char ch = ' ';
File * f = fopen(filename, "r");
if (f == NULL)
return 0;

//mdyn"comment~""comment~"\n
br = fread(nBuffer, sizeof(DWORD), 4, f);
m = nBuffer[0]; //so far always correct
d = nBuffer[1]; //if date was the 26th of the month, == 0
yr = nBuffer[2]; //if date was the 26th of the month, == 0
n = nBuffer[3]; //if date was the 26th of the month, == 0

fclose(f);
return 0;
}


You are reading binary data, not text. So you should open the file in
binary mode instead of text mode, by changing the fopen call to:
FILE *f = fopen(filename, "rb");

Character code 26 is an end-of-file marker in DOS text files. fread
notices this character and stops reading the file. (Before you fix the
fopen call, you should check the return value of fread, to confirm that
it is reading just one byte instead of four.)

--
David Olsen
qg********@yahoo.com

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.