Click here to Skip to main content
15,888,286 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: API needed Pin
David Crow23-Jun-06 10:00
David Crow23-Jun-06 10:00 
AnswerRe: API needed Pin
act_x23-Jun-06 10:05
act_x23-Jun-06 10:05 
AnswerRe: API needed Pin
Hamid_RT23-Jun-06 21:08
Hamid_RT23-Jun-06 21:08 
QuestionBinary File IO Pin
Jumpin' Jeff23-Jun-06 9:06
Jumpin' Jeff23-Jun-06 9:06 
AnswerRe: Binary File IO Pin
David Crow23-Jun-06 9:10
David Crow23-Jun-06 9:10 
GeneralRe: Binary File IO Pin
Jumpin' Jeff23-Jun-06 9:41
Jumpin' Jeff23-Jun-06 9:41 
GeneralRe: Binary File IO Pin
David Crow23-Jun-06 9:53
David Crow23-Jun-06 9:53 
GeneralRe: Binary File IO [modified] Pin
Zac Howland23-Jun-06 10:02
Zac Howland23-Jun-06 10:02 
Just a recommendation:

Instead of assuming your string is X number of characters long, write the string length first, followed by the characters in the string:

struct FileData<br />
{<br />
char* String;<br />
short MajorVersion;<br />
short MinorVersion;<br />
};<br />
<br />
// to write the file<br />
FileData data;<br />
// assuming data has already been filled in<br />
FILE pFile = fopen(..., "wb");<br />
long size = strlen(data.String);<br />
fwrite(&size, sizeof(size), 1, pFile);<br />
fwrite(data.String, size, 1, pFile);<br />
fwrite(&data.MajorVersion, sizeof(short), 1, pFile);<br />
fwrite(&data.MinorVersion, sizeof(short), 1, pFile);<br />
<br />
// to read the file<br />
FileData data1;<br />
FILE pReadFile = fopen(..., "rb");<br />
long readSize = 0;<br />
fread(&readSize, sizeof(readSize), 1, pReadFile);<br />
assert(readSize > 0);<br />
// create and initialize the new string<br />
data1.String = new char[readSize + 1];<br />
memset(data1.String, 0, readSize + 1);<br />
fread(&data1.String, readSize, 1, pReadFile);<br />
fread(&data1.MajorVersion, sizeof(short), 1, pReadFile);<br />
fread(&data1.MinorVersion, sizeof(short), 1, pReadFile);<br />


If you write the structure as a class, you can put the serialization code as a class member and have your code just call MyClass.Read/Write to make it very simple.

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

-- modified at 16:03 Friday 23rd June, 2006
AnswerRe: Binary File IO [modified] Pin
Joe Woodbury23-Jun-06 9:25
professionalJoe Woodbury23-Jun-06 9:25 
GeneralRe: Binary File IO Pin
Jumpin' Jeff23-Jun-06 9:50
Jumpin' Jeff23-Jun-06 9:50 
GeneralRe: Binary File IO Pin
David Crow23-Jun-06 9:55
David Crow23-Jun-06 9:55 
GeneralRe: Binary File IO Pin
Jumpin' Jeff23-Jun-06 9:59
Jumpin' Jeff23-Jun-06 9:59 
GeneralRe: Binary File IO Pin
Joe Woodbury23-Jun-06 10:37
professionalJoe Woodbury23-Jun-06 10:37 
QuestionEntry Points into Exe files Pin
Hibelton23-Jun-06 8:53
Hibelton23-Jun-06 8:53 
AnswerRe: Entry Points into Exe files Pin
ovidiucucu24-Jun-06 7:22
ovidiucucu24-Jun-06 7:22 
QuestionPointers [modified] Pin
Jay0323-Jun-06 8:16
Jay0323-Jun-06 8:16 
AnswerRe: Pointers Pin
Jun Du23-Jun-06 8:52
Jun Du23-Jun-06 8:52 
GeneralRe: Pointers Pin
Jay0323-Jun-06 9:19
Jay0323-Jun-06 9:19 
GeneralRe: Pointers Pin
Zac Howland23-Jun-06 10:08
Zac Howland23-Jun-06 10:08 
GeneralRe: Pointers Pin
Jun Du23-Jun-06 10:30
Jun Du23-Jun-06 10:30 
GeneralRe: Pointers Pin
Jun Du23-Jun-06 10:15
Jun Du23-Jun-06 10:15 
Questionaccessing an ftp server Pin
LCI23-Jun-06 8:15
LCI23-Jun-06 8:15 
AnswerRe: accessing an ftp server [modified] Pin
LCI23-Jun-06 10:39
LCI23-Jun-06 10:39 
QuestionTrapping the Mouse Pin
Scott P. Chapman23-Jun-06 7:42
Scott P. Chapman23-Jun-06 7:42 
QuestionNetscape plugin question (C++) Pin
Dennis Gourjii23-Jun-06 7:14
Dennis Gourjii23-Jun-06 7:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.