How to write and read a binary file with a text heading?

I need to write and read a binary file but hope the begining few lines of the file are readable text to explain the binary contents.
Who know how to write and read this kind of file by VC++?

Thanks,
jianjianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

graham_kCommented:
why not open the file first in text mode, to see. Then clsoe & reopen in binary mode if its what you think taht is ?
0
jianjianAuthor Commented:
Then, how do you know the position to start to read the binary? The text heading is 5 lines with fixed length of last line. After the last chrarcter is the begin position of binary. But how to position file pointer to start from here?
how to write it?
0
KangaRooCommented:
Open in text, find the position with ftell(). Close open in binary, go to the position:

FILE* file = fopen("filename", "rt");
// scan to the last position
int position = ftell(file);
fclose(file);
file = fopen("filename", "rb");
fseek(file ,position, SEEK_SET);
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

ehartantoCommented:
May be you can use the fstream class.

first you have to include:
#include <fstream.h>

ifstream finput("filename.bin", ios::bin);

and you can read it with:

finput.read(....); // sorry, I forgot the syntax.  Please check the help file

good luck,
erwin
0
PIGCommented:
When read from file :
FILE* file = fopen("YOURFILE.BIN", "r+");
int calcSize = 4*fixedLineSize + fixedLastLineSize;
char* buffer = new char[calcSize+1];
memset(buffer, 0, calcSize+1);
int readBytes = fread(buffer, calcSize, calcSize + 1, file);
BYTE* binData = new BYTE[4096];
// for example 4096 cna more or less
while (TRUE){
  readBytes = fread(binData, 4096, 4096, file);
   /*
      your code with concat binData
      You read just readBytes size;
   */
  if (readBytes < 4096) break;
}
When write to file is similar. Forgot for bynary mode and some old practice.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PacmanCommented:
jianjian,

if I understood your question what you want is a file which contains some readable text-info in the header.

If the user wants to view the file, with TYPE-command for example, he would get only the short info text and no "binary trash".

To do this open your file in binary mode and write your
information text into the file. Then write a EOF (end of file) signature which is #26.

example:

"this is a binary file. Don't type it ...\0x1A"

regards
Pacman
0
KangaRooCommented:
Ah that's a possibilty... Like Borland did.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.