Мне нужно написать программу на C, которая подсчитывает, сколько чисел содержится в файле. Предположим, данный файл указан ниже
ФАЙЛ:
Lorem ipsum dolor sit amet 103 consectetur adipiscing elit. 103.55
Phasellus nec neque posuere 103.55e-67 nulla sagittis efficitur.
ВЫХОД:
There are 3 numbers in file.
, которые составляют 103, 103,55 и 103,55e-67.
Я понимаю, что в C я могу читать символ за символом, используя fgetc()
, повторяя до EOF.Как можно получить последовательности цифр, как в приведенном выше выводе.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
FILE * file;
char path[100];
char ch;
int numbers[200];
int number_count;
printf("Enter source file path: ");
scanf("%s", path);
file = fopen(path, "r");
if (file == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
/* Finding and counting numbers */
while ((ch = fgetc(file)) != EOF){
// What logic do i write here??
}
printf("The file has %d numbers.\n", number_count);
return 0;
}