Problem probably about using fwrite-fread with directly structs

Hello everybody,

I am working on WinXp and Visual C++ 6.0 edition. I have two structs as
shown at the very bottom of my message. Besides my main formview, I
have a dialog box whose user interface is used to change the data
related with these structs. There is 1 "FileSetupData" struct and 20
FileThreatData to be saved and to be loaded back when the user
activates the dialog again. As an electronics engineer I am very used
to working with C functions fopen, fclose, fread, fwrite etc... and I
saved the data to a BINARY file by directly saving structs using
fwrite.
There are a total of 230 user interface items in the dialog. Strange
thing is; I can save and load back all the variables except one of
them. There is no error in the code. I used the same code for every
control and variable. For the FileThreatData struct, I can load back 19
of the simulated threats also can load back all of the varibles of the
20th threat except
"unsigned short int threat_priority"  variable of the struct in the
20th one.
Its value is loaded back as 0xCCCC. Read from the file as 0xCCCC too.
But before writing it to file, I control the variable and its value is
the value I assigned to it.

First I thought there is sth wrong with DDX variable of the associated
control. But there is no such a problem.
Then I thought sth wrong with file IO and write sth more(just dummy
data) at the end of the file. It didn't work .FileSetupData is
correct, 19 threats are correct. Only one variable of the last threat
is still wrong.
Then I thought the "bool" varibles inside the struct can cause a
problem, so changed them to unsigned int and change the program
accordingly. It didn't work still. FileSetupData is correct, 19
threats are correct. Only one variable of the last threat is still
wrong.
Then I changed the position of the "unsigned short int
threat_priority;"  varible inside the struct. This time again
FileSetupData is correct, 19 threats are correct. But the remaining
fields inside the struct under the variable "threat_priority" are
all read back as 0xCCCC or 0xCCCCCCCC

Then, I decided to read from this discussion forum about "saving
structs directly to binary files." As I guessed I found out the bit
alignment issue. From settings of the project I changed the bit
alignment to 1. Then I changed the bit alignment using pragma directive
as Joseph Newcomer suggests.  But these didn't work too... (By the
way I want to thank to Joseph Newcomer because I really learnt a lot
from his messages and website)

Finally I assigned everything to normal varibles (arrays os bools,
unsigned short ints, unsigned ints, ints etc.)... Save these
"normal" varibles one by one and read them back one by one. BUT the
problem is still the same......

What is wrong????


struct FileSetupData{
 unsigned int WarmupTime;
 unsigned int PBITTime;
 unsigned short int HwVersion;
 unsigned short int SwVersion;
 unsigned short int IcdVersion;
 unsigned short int GvdValidities;
 unsigned short int GvdVersion1;
 unsigned short int GvdVersion2;
 unsigned short int GvdVersion3;
 unsigned short int GvdVersion4;
 unsigned short int GvdVersion5;
 unsigned short int GvdVersion6;
 unsigned short int GvdVersion7;
 unsigned short int GvdVersion8;
 unsigned short int NoOfThreats;
 unsigned short int RtAddress;
 unsigned short int BlankingTimeout;
 unsigned short int IBITTime;
};
#pragma pack

#pragma pack(1)
 struct FileThreatData {
 unsigned short int band;
 short int AOA;
 unsigned int PRF;
 unsigned int start_time;
 unsigned int duration;
 unsigned short int threat_no;
 unsigned short int threat_priority;
 bool identified_or_not;
 bool inAgeoutTime;
};

0
11/8/2006 9:55:49 AM
vc.mfc 33608 articles. 0 followers. Follow

1 Replies
547 Views

Similar Articles

[PageSpeed] 32

0xCCCCCCCC means you have written an uninitialized local variable from a debug version of
the executable.

Are you really working in binary mode?  If, for example, a sequence 0x0A appears in your
data, the the file thinks it is in text mode, it will mistakenly convert it to 0x0D0A and
change alignment.

I gave up on writing binary files years ago, and would actually write either text files or
better still XML files for such purposes.  In fact, for trivial little configuration files
like this, given a choice between binary file representation and nearly any other
alternative, I consider raw binary data to be the single worst choice under nearly all
scenarios I can imagine.  Binary files don't start to make sense until you are into tens
of megabytes per second of data transfer.

Have you opened the file using a binary editor and looked at the contents?  That would
tell you if the problem was on output or on input.

Given a structure of sizeof() == n, and k instances to write, your output file should be
exactly k*n bytes in length.  If it isn't, there's something wrong with your output logic.

A typical mechanism you might try is
TRACE("Before: At %d\n", ftell(f));
int n = fread(&var, sizeof(struct), 1, f); // or some theme on these parameters
ASSERT(n == sizeof(struct));  // did we read as many bytes as we expected?
TRACE("After: At %d (%d)\n", ftell(f), sizeof(struct));

If there's something odd going on, this will possibly shed some light on it.
						joe


On 8 Nov 2006 01:55:49 -0800, "ocelikoglu" <ocelikoglu@gmail.com> wrote:

>Hello everybody,
>
>I am working on WinXp and Visual C++ 6.0 edition. I have two structs as
>shown at the very bottom of my message. Besides my main formview, I
>have a dialog box whose user interface is used to change the data
>related with these structs. There is 1 "FileSetupData" struct and 20
>FileThreatData to be saved and to be loaded back when the user
>activates the dialog again. As an electronics engineer I am very used
>to working with C functions fopen, fclose, fread, fwrite etc... and I
>saved the data to a BINARY file by directly saving structs using
>fwrite.
>There are a total of 230 user interface items in the dialog. Strange
>thing is; I can save and load back all the variables except one of
>them. There is no error in the code. I used the same code for every
>control and variable. For the FileThreatData struct, I can load back 19
>of the simulated threats also can load back all of the varibles of the
>20th threat except
>"unsigned short int threat_priority"  variable of the struct in the
>20th one.
>Its value is loaded back as 0xCCCC. Read from the file as 0xCCCC too.
>But before writing it to file, I control the variable and its value is
>the value I assigned to it.
>
>First I thought there is sth wrong with DDX variable of the associated
>control. But there is no such a problem.
>Then I thought sth wrong with file IO and write sth more(just dummy
>data) at the end of the file. It didn't work .FileSetupData is
>correct, 19 threats are correct. Only one variable of the last threat
>is still wrong.
>Then I thought the "bool" varibles inside the struct can cause a
>problem, so changed them to unsigned int and change the program
>accordingly. It didn't work still. FileSetupData is correct, 19
>threats are correct. Only one variable of the last threat is still
>wrong.
>Then I changed the position of the "unsigned short int
>threat_priority;"  varible inside the struct. This time again
>FileSetupData is correct, 19 threats are correct. But the remaining
>fields inside the struct under the variable "threat_priority" are
>all read back as 0xCCCC or 0xCCCCCCCC
>
>Then, I decided to read from this discussion forum about "saving
>structs directly to binary files." As I guessed I found out the bit
>alignment issue. From settings of the project I changed the bit
>alignment to 1. Then I changed the bit alignment using pragma directive
>as Joseph Newcomer suggests.  But these didn't work too... (By the
>way I want to thank to Joseph Newcomer because I really learnt a lot
>from his messages and website)
>
>Finally I assigned everything to normal varibles (arrays os bools,
>unsigned short ints, unsigned ints, ints etc.)... Save these
>"normal" varibles one by one and read them back one by one. BUT the
>problem is still the same......
>
>What is wrong????
>
>
>struct FileSetupData{
> unsigned int WarmupTime;
> unsigned int PBITTime;
> unsigned short int HwVersion;
> unsigned short int SwVersion;
> unsigned short int IcdVersion;
> unsigned short int GvdValidities;
> unsigned short int GvdVersion1;
> unsigned short int GvdVersion2;
> unsigned short int GvdVersion3;
> unsigned short int GvdVersion4;
> unsigned short int GvdVersion5;
> unsigned short int GvdVersion6;
> unsigned short int GvdVersion7;
> unsigned short int GvdVersion8;
> unsigned short int NoOfThreats;
> unsigned short int RtAddress;
> unsigned short int BlankingTimeout;
> unsigned short int IBITTime;
>};
>#pragma pack
>
>#pragma pack(1)
> struct FileThreatData {
> unsigned short int band;
> short int AOA;
> unsigned int PRF;
> unsigned int start_time;
> unsigned int duration;
> unsigned short int threat_no;
> unsigned short int threat_priority;
> bool identified_or_not;
> bool inAgeoutTime;
>};
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
11/8/2006 1:10:07 PM
Reply:

Similar Artilces:

300 Locations+ using RMS?
Does anyone out there have 300-400 locations operating on RMS? Is this even possible. Currently we have 80 with a projected 150 next year and 200 following. Within 5-10 years we will hit 500-1,000. Can RMS support a company of our size? Has anyone pushed the system to this level? -- Jocelyn wow :) Is there a HQ Dongle with license up to 1000+ locations...your company is rocketing :) Is your company in fashion retail ?Food? Your implementation is quite big. how was the performance of RMS hitting 80 locations? Are you using HQ (I suppose you do.)? how's the speed in terms of conne...

How do I insert a radio button using Excel 2007?
How do I insert a 2 radio button group using Excel 2007 and of course how do i program them? thank you in advance. The following code will create a Frame and two Option Buttons at cell C3. Change the various Top and Left properties to get them positioned as you want. Sub AAA() Dim FRA As OLEObject Dim BTN1 As OLEObject Dim BTN2 As OLEObject Dim WS As Worksheet Dim TopLeftCell As Range Dim VBComp As VBIDE.VBComponent Dim CodeMod As VBIDE.CodeModule Dim N As Long Set WS = Worksheets("Sheet1") Set TopLeftCell = WS.Rang...

Report Writer
I know report writer is not agreat tool and Crystal is an option. However, I would like to achieve this in report writer. I simply want some 'boxes' and 'lines' on my invoice so that the fields 'appear' as if they are in a table. I can get lines and boxes in, but how do I get the box to extend down to the top of my report footer? Have a look at the SOP Blank Invoice for an example. In a graphic report, you can draw a box by adding the top horizontal line to the page or report header and then add vertical lines that start in the same section but are as long a...

Office apps won't open, report Office in use by another use
Version: 2004 Operating System: Mac OS X 10.4 (Tiger) Processor: Intel A few days ago, our Office applications started showing the report that "An Office program is being used by "user." your installation exceeds the number of installations permitted by thelicense agreement. Click More Information to learn about Microsoft Office licensing." But OFFICE ISN'T BEING USED BY ANYONE. All computers have been restarted to no avail. HELP! <sakas@officeformac.com> wrote: > A few days ago, our Office applications started showing the report that > "An Office p...

Problem Veiwing Reports in CRM 3.0
Hi, I am having problems viewing reports in CRM Webclient. I log on as a standard user click on the report edit the filter and generate the report, and the system says the page cannot be displayed. If i log on as administrator it works, so i am assuming it is a permissions issue. Where and how to i change this so all users can view and generate reports? Any help on this issue would be much appreciated, thanks in advance Hi, Now the administrator cannot view the reports, apart from going to the report server via IP. Any help would be great. "Patrickh" wrote: > Hi, > ...

Using auto
Hi, Good Day to everyone... I 'am currently using Microsoft Outlook 2003...My Boss wants me to make accounts that will be able to send and receive messages....I have been trying to do it manually, but when I try to do the send/receive messages an error occurs. Someone explained to me to do a Auto Configuration using the cpanel of the site....I tried it but then I don't know how to use it.....in the Tools menu the send and receive button disappeared.....i have no idea on what is currently happening.....and I can't make sure if the mail accounts have been add to outlook e-m...

Having Formatting Remain on "Called" cell using IF function
I'm using an IF function to call some other cells, but I want to have the various words color coded, eg ("Certified" is green, "Recertify" is yellow, and "Not Certified" is red). I have the IF function working correctly, but when it "calls" the cell to have it display the appropriate word, the color of each word is not brought along with it...only the text. Is there a way to be able to color code it so that the appropriate color appears? Thanks! -- Zaraf ------------------------------------------------------------------------ Zaraf's Profi...

phone textbox default value problem
I have a phone field and I have a default area code set and when I tab to the field the area code is highlighted and I have to click off of it to enter data. How can I have it so when I tab to the field I can immediatly start typing the phone number after the area code? If the area code is different than the default I figure I could just back space and change it. 90% of the time the default will be correct. I have the input mask as !\(999") "000\-0000;0;_ and the default value as (916), I am leaving the formatting info in because I have to import the data and it has the...

"Theme" and "recently used" font list
How do I change the "theme" and "recently used" font list? "WD2000: How to Turn off the Font Most Recently Used (MRU) List" at http://support.microsoft.com/?kbid=199785; for Word 2002, see http://support.microsoft.com/?kbid=289506; for Word 2003, see http://support.microsoft.com/?kbid=820917. -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> &g...

use of countif()
Hi. Does anyone knows how can apply the countif() function partial? (for ex. If i have the column A10:A100 to apply countif() in the follow cells A54, A67, A72:A79 , A87, A91:A95, A98.) If that impossible, any other idea? Thanks. One way =SUMPRODUCT(COUNTIF(INDIRECT({"a2","a5","a8","e10"}),"a")) -- Don Guillett SalesAid Software dguillett1@austin.rr.com "nikos" <nikos@discussions.microsoft.com> wrote in message news:0702BFB4-B60A-42D4-8B04-C5D055CC5072@microsoft.com... > Hi. > > Does anyone knows how can appl...

Getting a LockMatchID error using sp_rename
When I'm trying to rename a table in the tempdb table, I'm getting an error when I run it in a stored procedure or code, but it works ok on MSSMS. I get the error: Invalid EXECUTE statement using object "Object", method "LockMatchID". EXEC sp_rename 'Tempdb.dbo.oldFile' , 'newFile' Don't ask me why I have to mess with the tempdb files (This is probably the problem). But I have old legacy code that writes to the tempdb database. On Jul 14, 10:00=A0am, Cindy Lee <danparker...@yahoo.com> wrote: > When I'm trying to renam...

Exchange 2000/2003 Routing Problems
I recently added a new Exchange 2003 server to our 1 server Exchange 2000 org. I am able to send mail from the new Exchange 2003 server to the 2000 server. But when I send to the 2003 server the mail does not go delivered. I am assuming this is a routing issue. How can I troubleshoot this? Should each server be in a seperate routing group and use a routing group connector? They are currently i n the same routing group. Thanks, Forest In case anyone has this problem my STMP connector was setup to use a smart relay. I had to check the box that said attempt local delivery before using...

MIME hdr problems
we send mail from Goldmine application through our exchange smtp server. A few emails are being sent back. The NDR is 5.7.1 but in the header I see this line: x-amavid alert" BAD HEADER MIME error: illegal encoding [base64] for mime type message /rfc822 I appreciate this is not necessarily an exchange issue but hoping someone might know something about this and what I steps i can take to solve it. sandgroper <sandgroper@discussions.microsoft.com> wrote: >we send mail from Goldmine application through our exchange smtp server. A >few emails are being sent back. The NDR...

dynamic charts using named formulas
I have dates in the first row I have values in the second row I use tecnique of named formuls of offset function (tushar Methta) so that the chart is updated when data for new dates are added. now after some time I want to delete the older data If I delete the columns of older dates the named formula get messed up and I get error message that three is some wrong reference. actulally in the offset formula the starting cell is relaced by "REF" Then I have to redo the whole exercise any solutions. If the first row is some sort of header that doesn't get deleted, use it as the ba...

free/busy problem, again
Sorry to bump this, but I really need to figure this out. Here's the origiinal question, (thanks, EdC for the original response) for clarification, "co-exist" with exchange 5.5 is what the exchange 2003 setup "wizard" that I was running from the CD called it. It walked you through the process in 30 easy steps! Anyway, there is just the one organization with the two servers: I have an exchange 5.5 server, which for various reasons, the schedule+ free/busy folders are gone/corrupted/not working. I set up a new exchange 2003 server in co-exist mode, and moved some...

Problem with installation of Microsoft Office Enterprise 2007
L.S. I have problem with re-installing Microfost Office Enterprise 2007. With the help of Google I discovered that a part of the problem were the remnants of the former installation that didn't show up in the "Software" window, and I removed them using Windows Install Cleanup. Now the installation process is proceeding further that before, but still cancels at a certain point, restarting the computer doesn't seem to help. In the "SetupExe"document in my \Temp map the following frase seems to point at the error in the installation: Log level chan...

Problem with backup of microsoft exchange information store
Having a problem with backup exec 10d backing up microsoft exchange information store. Worked yesterday, but not today. Error is as below. Anyone have any ideas? Thanks Job ended: Wednesday, August 23, 2006 at 8:49:28 PM Completed status: Failed Final error: 0xe0008488 - Access is denied. Final error category: Security Errors For additional information regarding this error refer to link V-79-57344-33928 Backup- \\NYMAIL03\Microsoft Exchange Information Store V-79-57344-33928 - Access is denied. Access denied to database . Hi, You should rally check with Veritas, but here is what a ...

Mail merge problem #3
Running Publisher 2003, Word 2003 I have a Word document -- with an Avery label format -- containing my mailing list. When trying to mail merge with the wizard, it errors out saying; "Some of the field names in this data source are blank or contain invalid characters, such as periods, brackets, or exclamation points. Change these field names before you import data from this data source." I've cleared out all the punctuation, etc. marks that are there and cleared out any blank "Labels". I'm still getting this error. What's up? Will I be able to make...

Invoice Designer portrait/landscape problems
I'm using Money 2004 Small Business edition with winXP. I go into Invoice Designer and open the template 'Service Portrait 1 (Logo)'. This opens, and gives me nearly enough to the invoice layout I would want subject to a few minor alterations. If I then save this template as a new template (with or without any alterations), open the invoice feature within Money and select this new template as the one I want to use, the print preview gives me a landscape orientation of this invoice. It also prints as landscape. (Choosing the original 'Service Portrait 1 (Logo)' templat...

OL 2003 and MAPPOINT problem
Using OL 2003 with Mappoint 2004. The Cut & Paste function using Ctrl-C and Ctrl-V is malfunctioning. When doing a cut & paste using the Ctrl functions, the MAPPOINT symbol is always PASTED into the new area (Mappoint has not been involked in the session to create a map to paste). IF I use the EDIT | COPY menu items and then the EDIT|Paste into the new area, the selected text is properly pasted. Displaying the Office Clipboard confirms what is happening, but WHY? Previously, I used OL 2002 with a prev version of Mappoint and this operation worked fine. Ideas?? or is this a ...

Outlook starting problem
"Cannot start Microsoft Outlook. A dialog box is open. Close it and try again." ...

MS Access query filter problem
I have created a fairly basic database in Access with approx 20 columns/fields in the primary table. I am trying to build several queries which filter the data differenty. When in "Design View" for the query every column/field can be sorted with standard criteria except for two. These two fields have one thing in common: unlike every other field which is populated "manually" via a form, these two are populated via pull-down menus (the values of which are sourced from separate tables). No matter what criteria I use, the query refuses to return any records...

Website email related problem
Our website is on the same internal network and shares the same domain name as our Exchange 2003 server (Win 2003 server environment). PROBLEM: if a website user clicks on an email (ex: mailto:info@vipclubber.com) using the same domain email address from the site domain, the email does not go through to the mailbox where it was originally sent. I am not sure if it is a DNS, Mail Server or alias problem. What should be done to ensure a successful delivery of any related email funstion from the website to the correct mailbox that shares the same domain? Please help! ...

using an NPV formula
=NPV(After_tax_investment_rate,I14:I23) The problem I am having is that the I14:I23 needs to vary. In one instance it needs to be I14:I50 another time I14:I20. Is there a way to automate that so it automatically sees the different length on the list and enters it within those brackets? Help..... :-) Thanks in advance. Tom See http://cpearson.com/excel/named.htm#Dynamic In article <gawFd.67880$Xk.16617@pd7tw3no>, "Tom" <tom.c@shaw.ca> wrote: > The problem I am having is that the I14:I23 needs to vary. In one instance > it needs to be I14:I50 anot...

Outlook Client "Problem communicating with CRM Server...
I have installed the light Desktop Outlook Client on a notebook. Everything works fine after the installation. Now the user wants a notebook client with local MSDE - so I�ve deinstalled the light client an installed the notebook client. I specifed the URL (not the IP) for the CRM server during the client installation. When I launch Outlook now I get the following error - "There is a problem communicating with the Microsoft CRM server. The server might be unavailable. Try again later. If the problem persists, contact your system administrator." Then it tells me that it cannot lo...