Write a function named iCount that counts the number of integers in a text file that only contains decimal integers. The function takes a single argument, a constant string that gives the name of the file. It returns an integer. If the specified file cannot be opened for reading, the function must return -1. Ignore the main.c file. Write your solution in the iCount.c file.

Respuesta :

Answer:

Below is the given solution

Explanation:

#include <stdio.h>

int iCount(char *fileName) {

   FILE *file;

   int num, count = 0;

   file = fopen(fileName, "r");

   if (!file) {

       return -1;

   }

   while (fscanf(file, "%d", &num) == 1)

       count++;

   fclose(file);

   return count;

}

int main(int argc, char *argv[]) {

   char filename[100];

   printf("Enter file name: ");

   scanf("%s", filename);

   printf("Number of integers in file is %d\n", iCount(filename));

   return 0;

}