Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to (1) Open an input file called \"in.txt for reading. The file

ID: 3742013 • Letter: W

Question

Write a program to (1) Open an input file called "in.txt for reading. The file in.txt" will be located in the same directory from which the executable from your program will be run. The file "in.txt" will contain a list of integers, one integer per line. If the file "in.txt" does not apen successfully, print a message to the screen that the file in.txt does not exist, and exit the program. One way to check if the file was successfully opened is the following. If the statement fpi-fopen("ssn_in.txt"."r does not successfully open the file "in.txt" for reading then the FILE pointer fpi is equal to the macro NULL. That is if(fpiNULL)printf"The file in.txt did not successfully open") (2) Prompt the user of your program to enter an integer and read-in that integer from the keyboard. (3) Read the integers in the file "in.txt" and count the number of occurrences of the user's integer in the file Also, count the total number of integers in the file "in.txt while you are reading them. 4) Write out a message to the user containing the total number of occurrences of the user's integer in the file. Also, indicate to the user the total number of integers that were in the file "in.txt" In arder to test your program, use Microsaft text editor notepad exe to create a file called in.tt. Write some integer values inta the file and place the file in the folder containing your program executable file ".exe. Alternatively, if you are running xcode, create a sample file as we did in lecture and in Program 01 assignment.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Code

#include<stdio.h>

int main(){

              //opening file in read mode

              FILE *fp=fopen("in.txt","r");

              if(fp==NULL){

                             //file couldn't be read

                             printf("in.txt does not exist!");

                             return 0;

              }

             

              int userInteger,readValue,count=0,totalIntegers=0;

              //getting an integer from user

              printf("Enter an integer: ");

              scanf("%d",&userInteger);

              //looping until there are no integers left to read in the file

              while(fscanf(fp,"%d",&readValue)==1){

                             //checking if the current integer is same as the user entered integer

                             if(readValue==userInteger){

                                           //match

                                           count++;

                             }

                             //incrementing the count of total integers

                             totalIntegers++;

              }

              //closing the file

              fclose(fp);

              //printing the stats

              printf(" %d occurred %d times in the file ",userInteger,count);

              printf("There are %d integers in the file ",totalIntegers);

              return 0;

}

//text file used

10 20 30 50 55 12 77 96 33 11 25 6 5 666 2 2 3 6 52 33 5 3 3 5 1 2 2 5 4 5 8 4 2 2 24 8 8 5 6 9

/*OUTPUT*/

Enter an integer: 2

2 occurred 6 times in the file

There are 40 integers in the file

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote