• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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,
0
jianjian
Asked:
jianjian
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now