Question: Please provide a C Language code only for the following 11.10 Lab Week 13 - File I/O Programming Tasks - categorize the values in a
Please provide a C Language code only for the following
11.10 Lab Week 13 - File I/O
Programming Tasks - categorize the values in a file
You may find it helpful to refer to the sample code we looked at in class. You can find it here: Sample File I/O Code
1) Prompt the user for a filename, if the file does not exist, exit the program after outputting to screen the following message:
could not open file
2) If the file does exist, open the file and read all of the integers in the file. To determine if you have read all of the integers, you must check if the end-of-file has been reached. This can be done using the feof() function, as described in the middle of section 9.5. The function feof() takes in a pointer to a FILE struct and returns true if the previous call to fscanf() did not return a value since the end-of-file was reached. For example, suppose a file contains 3 integers,
8 45 -2
After the file is opened with fopen(), the first call to fscanf() returns 8. The second call to fscanf() returns 45. The third call to fscanf() returns -2. Finally, a fourth call to fscanf() returns EOF. [Note: of course there will be arguments entered in the calls to the functions, but that is left up to you to determine].
Thus, continue reading in ints while the returned value of fscanf() is not EOF, i.e. feof() continues to return false.
3) Count the number of integers read in from the file, and print the result to the screen.
4) Count the number of positive, negative, and zero values read in from the file, and print the result to the screen.
5) Count the number of even and odd values read in from the file, and print the result to the screen.
Sample Output
When the filename labw13_data.txt (you can download the file and/or view its content from just above the coding box) is entered by the user, the following output is printed to screen.
Enter a filename: There were 16 values read in of which 15 postive, 0 negative, and 1 zero AND 9 even, 7 odd.
file names
labw13_data.txt
labw13f1.txt
labw13f2.txt
In class sample File I/O Code example
#include
//count number of elements in data between lowerBound and upperBound
int countNum(int lowerBound, int upperBound, int data[], int size){
int count = 0;
for (int i = 0; i < size; ++i) {
if (lowerBound <= data[i] && data[i] < upperBound){
count++;
}
}
return count;
}
//prints a line of asterisks (count = # of asterisks)
void printStars(int count){
for (int j = 0; j < count; ++j){
printf("*");
}
return;
}
int main() {
int numInts = 100;
int data[100];
FILE* myFile = NULL; //File pointer
int curNum;
myFile = fopen("randints.txt","r");
if (myFile == NULL) {
printf("Could not open file randints.txt. ");
return -1; // -1 indicates error
}
for (int i=0; i fscanf(myFile,"%d", &curNum); data[i] = curNum; } fclose(myFile); printf("%d ",data[0]); printf("%d ",data[1]); printf("%d ",data[2]); printf("%d ",data[99]); //ploting Histogram of numbers int binSize = 10; for (int i = 0; i < 100; i+=binSize){ printf("%2d-%2d:",i,i+binSize-1); printStars(countNum(i,i+binSize-1,data,numInts)); printf(" "); } printf(" "); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
