Read the binary number from the user and store it into a C string (i.e. char[ ])
ID: 1814643 • Letter: R
Question
Read the binary number from the user and store it into a C string (i.e. char[ ])
o The c string shall be able to accommodate a 32 bit binary number, note each digit in the binary number is a character, and there must be a null character at the end to terminate the string
o In the event that the user does not enter a legitimate binary number, print an error to the console and exit
? Convert the binary number into a decimal number
? Since the binary number is a string, you do not need to shift the number as before to access individual bits
Explanation / Answer
FILE *fp = fopen("mybinfile.ttt", "wb"); char drumCString[6] = "Hello "; printf("%s ", drumCString); //the string length + 1 for the null terminator unsigned short sizeOfString = strlen(drumCString) + 1; fwrite(&sizeOfString, sizeof(unsigned short), 1, fp); //write the string fwrite(drumCString, sizeof(char), sizeOfString, fp); fclose(fp); fp = fopen("mybinfile.ttt", "rb"); unsigned short stringLength = 0; fread(&stringLength, sizeof(unsigned short), 1, fp); char *drumReadString = malloc(sizeof(char) * stringLength); int count = fread(&drumReadString, sizeof(char), stringLength, fp); //CRASH POINT printf("%s ", drumReadString); fclose(fp);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.