Hello! I'm reading in a .txt file into a program and I'm trying to get it to count how many letters are in each line, and then display them. I'm not exactly sure what to use though.
Here's my code so far:
Code:
#include <stdio.h>
#include <string.h>
int countLetters();
void main()
{
countLetters();
}
int countLetters()
{
char c;
FILE *infile = fopen("FILEIOLabDATAFILE.txt", "r");
if(infile == NULL)
{
printf("The file was not successfully open.\n");
return;
}
}
I'm not sure if I would use
Code:
char letter;
fscanf(infile, "%s", &letter);
or
Code:
char line[80];
infile.getline(line, 80, ‘\n’);
fscanf(infile, "%s", letter);
And then I'm not exactly sure what variable I would put to display it? Something like this?
Code:
int i;
for(i = 0; i < strlen(line); i++)
{
if(line[i] == ‘x’) { count++; }
}
Any help would be great! Thanks!