Need this coded in C, no C++ functions can be used. Can include stdio.h, stdlib.
ID: 3685445 • 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.
The function needs to be able to read the first 88(no more) valid numbers in a file into a single dimensional array.
If the program encounters a non-numeric value in the file, it should flush that input line in the file, print an error message and go to the next line. It should not use any numbers from that line.
If the program can't open the file or if no file name is entered (^d is entered on Linux or ^zEnter on Windows for EOF), it should print an error message and exit.
This function needs to be seperate from main and send the array to main using pointers.
Explanation / Answer
//Read Data from file using functions
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define rows 2
#define cols 3
void main()
{
FILE *fptr;
char filename[15];
char ch;
double dataRead;
printf("Enter the filename to be opened ");
scanf("%s", filename);
/* open the file for reading */
fptr = fopen(filename, "r");
//passing the file through function
void fileRead( FILE *fptr);
}
void fileRead(FILE *fptr){
if (fptr == NULL)
{
printf("Cannot open file ");
exit(0);
}
for(i = 0; i < rows;i++)
{
for (j = 0 ; j < cols; j++)
{
fscanf(fptr,"%lf",&dataRead);
printf("%.15f ",dataRead);
}
printf(" ");
}
fclose(fptr);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.