Thursday, July 9, 2009

Standard C programming question, binary files???

I have a binary file and have it saved as a .txt file from notepad, is there a way to save it as a binary file? My code below should open the binary file, and print the first byte, or 8 bits, 1 or 0's right ? however i get like -57, and -88's... I think it's because the file is not saved as binary but any help would be appreciated. Basically HOW DO I SAVE A FILE AS BINARY? is it .bin ?








#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;





#define INPUT_FILE "CodedText.txt"





int main(void)


{


FILE *fp;


char byte[8]; /* holds a byte of information */


int i; /* counter in test */


fp = fopen(INPUT_FILE, "rb");


if (fp == NULL)


{


printf("Can't open %s\n", INPUT_FILE);


getchar ( );


exit(EXIT_FAILURE); /* or any non zero value*/


}


/* perform desired operations on the file */





fread (byte, 1, 1, fp);


for (i = 0; i %26lt; 8; i++)


printf("%d\n", byte[i]);





getchar ( );





fclose(fp);


return 0;


}

Standard C programming question, binary files???
A binary file is a specially coded file. If you are saving it as .txt then it is a text file. You need to write a program specifically to read and write binary files. Notepad doesn't save files in binary format.
Reply:Just write a simple code that will show up the txt file. That's how I do it.


No comments:

Post a Comment