Link to home
Start Free TrialLog in
Avatar of hmk
hmk

asked on

convert from binary to text

hai ,
 could anyone suggest me how to convert binary file to text file without loosing information.
is theire any function in C to convert each byte to ASCII format.
i want load all binary file in to a buffer and convert it into text format.
Avatar of Triskelion
Triskelion
Flag of United States of America image

UUENCODE
/*
start with a file called file1.bin that contains your binary data.  Run this program over that file and a new file called fiile2.txt will be created containing an ascii representation of the old file.  Then a new file called file3.txt will be created containg by converting the text back to binary.
*/
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char BYTE;

void main(void)
{
      FILE      *      hInFile;
      FILE      *      hOutFile;
      BYTE            byteData;
      BYTE            byteArray[3];
            
      if (NULL == (hInFile=fopen("file1.bin", "rb")))
            {
            puts("cannot open file1.bin");
            exit(1);
            }
      if (NULL == (hOutFile=fopen("file2.txt", "wb")))
            {
            puts("cannot open file2.txt");
            exit(2);
            }
      while(!feof(hInFile))
            {
            fread(&byteData, 1, sizeof(byteData), hInFile);
            if (!feof(hInFile))
                  {
                  fprintf(hOutFile, "%03d", byteData);
                  }
            }
      fclose(hOutFile);
      fclose(hInFile);
      /* put it back*/
      if (NULL == (hInFile=fopen("file2.txt", "rb")))
            {
            puts("cannot open file2.txt for read access");
            exit(3);
            }
      if (NULL == (hOutFile=fopen("file3.bin", "wb")))
            {
            puts("cannot open file3.bin for write access");
            exit(4);
            }
      while(!feof(hInFile))
            {
            static char spTempString[4];
            fread(&byteArray, 1, sizeof(byteArray), hInFile);
            
            if (!feof(hInFile))
                  {
                  memcpy(&spTempString, &byteArray, 3);
                  spTempString[3]='\0';
                  fprintf(hOutFile, "%c", atoi(spTempString));
                  }
            }
      fclose(hOutFile);
      fclose(hInFile);
}
Avatar of Ready4Dis
Ready4Dis

I have some code also.
Avatar of hmk

ASKER

hai triskelion,

i am sorry i mislead the question, what i want is to convert binary file in to text file with actual alphanumeric charecters as a text not in ASCII format txt file.
This binary file is actually a Btrieve file consists spaces,Control breaks,charecters and........
could u send me how to convert this btrieve file in to a text file.
The previous program was worked fine.
hope u find the solution,
Thankyou,
hmk.


What will you do with the non-printable characters?
1) What version of BTrieve?
2) Do you have the BTrieve driver loaded?
3) If >= version 5, can you use C++ and ODBC?
If you want to just dump the contents into a text file, you will need to either use the BTrieve library with proper calls, read the data with the proper lookup function and fprintf the data into your output text file.  If you do not have the driver and the version is < 5, the you will have to examine the file layout and create your own routines that do the lookups.
ASKER CERTIFIED SOLUTION
Avatar of hougaard
hougaard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you're asking if you have a value like a long
that you have read in (in binary form) and you want to save it in a text compatible file, you can have something like this

{
   auto long lCustNum;
   auto int  iValue;
//Open file up here
//Read in data in binary format
...
   fprintf(hOutFile, "%ld %d", lData, iValue);//Save data in text form
...
//close file down here
}
All are kotting
Avatar of hmk

ASKER

sorry ,please delete the question none of above solutions work..
Thanks for your valuable advice


hmk