Я пытаюсь перенаправить файл в текстовый документ, и мой код читает его и выводит количество символов, присутствующих в коде, и позволяет пользователю вводить букву, не чувствительную к регистру, которая указывает программе просматривать текст ивозвратите количество раз, когда письмо появилось.
Scanf не работает правильно и не получает пользовательский ввод.
Я пытался добавить пробел, подобный этому: "% c"и "% c \ n".Попробовал удалить & и использовать getchar ();
#include <stdio.h>
// Defining variables
#define SIZE 8000
int main(void)
{
char case1 = 'a', case2 = 'b', data[SIZE];
int i, count;
// Reading in the .txt file into a 8000 sized array and removing any other unnecessary portions from the array.
for (i = 0; i < 8000; i++)
{
fscanf(stdin, "%c", &data[i]);
}
printf("The text from the file: \n\n");
for (i = 0; data[i] != '\0'; i++)
{
printf("%c", data[i]);
}
// Printing out the .txt document and counts the number of characters
printf("\n");
printf("There are a total of %i characters in that text.\n ", i+1);
printf("Enter a character to search for in the text:\n ");
// Exiting from stdin.
if (!freopen("/dev/tty", "r", stdin))
{
perror("/dev/tty");
exit(1);
}
// Asking for user input.
scanf("%c", &case1);
// Converting user input into different lettercase.
if (case1 == toupper(case1))
{
case1 = tolower(case1);
} else
{
case2 = toupper(case2);
}
printf("This is your input: %c. Also searching for: %c.\n", case1,case2);
// Searching the text for user-inputted letter.
for (i = 0; data[i] != '\0'; i++)
{
if (data[i] == case1)
{
count++;
}
else if (data[i] == case2)
{
count++;
}
}
printf("That letter %c appears a total of %i times.\n", case1, count);
return 0;
}