Click to See Complete Forum and Search --> : converting to binary file
Shiney Vang
January 22nd, 2002, 05:25 PM
I have a file that needs to be converted to a binary file. To do this I understand I'll need to open a binary file pointer. However, when I do a
xchar = fgetc(infile); // infile is non-binary
// file pointer
How do I make xchar into a binary so it can be written into the the binary file?
qg89944
January 23rd, 2002, 10:09 AM
How about this:
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *inFile = NULL;
FILE *outFile = NULL;
if((inFile = fopen("textfile.txt", "rb")) == NULL)
{
fprintf(stderr, "Can't open file textfile.txt to read.\n");
exit(1);
}
if ((outFile = fopen("binfile.bin", "wb")) == NULL)
{
fprintf(stderr, "Can't opne file binfile.txt to write.\n");
exit(1);
}
int currChar;
while((currChar = fgetc(inFile)) != EOF)
fputc(currChar, outFile);
fclose(inFile);
fclose(outFile);
}
NMTop40
January 23rd, 2002, 10:19 AM
Under Windows/DOS, all the program will do is change \r\n line endings to \n
If that's all you require then it's fine.
If the text file contains data such as numbers in text format and you want to store them in a binary format then it is far more complex. In that case you will need to know the format of the input file and parse it, then write it out to a pre-defined binary format.
The best things come to those who rate
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.