Need this coded in C, no C++ functions can be used. Can include stdio.h, stdlib.
ID: 3685724 • Letter: N
Question
Need this coded in C, no C++ functions can be used. Can include stdio.h, stdlib.h, string.h, and ctype.h.
I need a function that will prompt the user for an input file name of a file containing data (integers and floats) that are four numbers to a line seperated by commas.
If the file name is invalid then an error msg should be printed and the program should exit
example of this error msg:
If no file name is entered an error msg should be printed and the program should exit
example of this error msg:
The function needs to be able to read up to the first 88(no more) valid numbers in a file and place them into a single dimensional array.
If the file has any non numerical data in a line that line of data is to be flushed and the program should continue reading at the next line but also keep track of which lines produced invalid data and print the following msgs when done reading:
The function should also count how many lines produced good data. Just a numerical total no need to remember each line.
The newly formed array and the total number of good lines should be passed to main so i may use it in other functions for the program. Do not worry about the file names just create a function that can do the tasks specified. I have the files it needs to read.
Your help would be greatly appreciated and if you do not have time to code this at least give me an outline that would be easy to follow for me to attempt to code myself.
Enter File Name: xyz Error: Could not open file xyz for read.Explanation / Answer
Note:Follow the below code it would be helpful to you. #include #include int main(int argc, char *argv[]) { int num, pos, count; FILE *fp; char (*array)[4096]; /* pointer to an array of buffers */ if (argc < 2) { printf("Usage headtail filename [number] "); return 1; } fp = fopen(argv[1], "r"); if (fp == NULL) { printf("Cannot open file %s ", argv[1]); return 1; } if (argc > 2) { /* get the number from the command line if 2 args were given */ if (sscanf(argv[2], "%d", &num) != 1) { num = -1; } } else { /* otherwise read from standard input */ if (scanf("%d", &num) != 1) { num = -1; } } if (num < 0) { printf("Invalid number "); /* negative or non numeric */ return 1; } /* allocate space for num+1 buffers */ array = malloc(4096 * (num + 1)); for (count = pos = 0; fgets(array[pos], 4096, fp) != NULL; count++) { /* printing the first num lines */ if (count = num + 1) pos = 0; } if (count > num) { /* more lines to print */ pos = count - num; if (pos > num) { /* print place holder for missing lines */ printf("... "); } else { /* print from the last line printed */ pos = num; } for (; posRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.