The Count3s program opens a binary file containing 32 bit integers (ints). Your
ID: 3598380 • Letter: T
Question
The Count3s program opens a binary file containing 32 bit integers (ints). Your program will count the number of occurrences of the value 3 in this file of numbers. Your objective is to learn about opening and accessing files and apply your knowledge of control structures. The name of the file containing data used by the program is "threesData.bin"
1. when you can verify that the file is open (hint), begin to count the int '3' until the end of file (hint) by use of a conditional statement. If you havent reached the end of the file, the file should remain open. 2. record the number of 3s by assigning it to a variable or some other method of display. 3. close the file.
Explanation / Answer
Implemented code in C languare as per the requirement, as no programming language is mentioned. Please comment if you want it in some other language. I'm expert in multiple languages.
#include<stdio.h>
int main(){
FILE *file = fopen("threesData.bin", "r");//Opening file for reading
int c,count=0;
while ((c = fgetc(file)) != EOF){ //Reading char by char
if((char) c == '3'){
count++; //Count 3's
}
}
fclose(file); //Closing file
printf("Count of the number 3: %d ",count); //Printing result
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.