Question
How would I create this C program? Open a text file containing the input in the form of integer numbers. Read those numbers from the
How would I create this C program?
Open a text file containing the input in the form of integer numbers. Read those numbers from the file and
store them in an integer array. Display the stored number in the integer array.
Use the following functions to implement the above problem.
int main (int argc , char* argv[]):
char* argv[] contains the input file name, if the file name is not provided then display the error message. If unable to open the file (in case of wrong file name) then also display (different) error message.
Here are some hints for error handling
a. If argc value is not equal to 2 that means file name is not provided by the user.
b. If file pointer has a value NULL (after fopen call) that means file could not be opened successfully.
int read_info (char *, int *):
This function takes a file name to read the input from and an integer array
to store the input from the file. It returns the size of the input which is the basically the number at the first
line of the input file (see below).This function opens the input text file if unable to open return -1 or any
negative indicating unable to open the file that can be used in the main to display the error message. If
able to open the input file successfully then return the size of the input.
Sample input file
5
112
335
421
611
220
Here 5 is the size of the input. Read this number first (using fscanf function). Then use it to run a loop to
read the input from the file and store them in the integer array.
Again there are multiple ways to read input from a file above suggestion is just one way. You are free to read input from the file using any other function /method.
void print_array (int *, int ) -
This function prints the content of the integer array. It takes an integer array and the size of the input.
Note
1. Implement the program using the function prototype as given in the document. They are sufficient and should not be modified.
2. Dont use any global variable in the implementation.
Sample outputs
Name of the input file is numbers.txt
./a.out
File name not provided
./a.out sample.txt
Unable to open the file
./a.out numbers.txt
User input
112
335
421
611
220
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started