|
-
January 22nd, 2002, 06:25 PM
#1
converting to binary file
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?
-
January 23rd, 2002, 11:09 AM
#2
Re: converting to binary file
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);
}
-
January 23rd, 2002, 11:19 AM
#3
Re: converting to binary file
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|