Ummm... fgetc returns an int... check it out in your library documentation.
There should be no reason to make copies of the existing variables, C is perfectly capable of comparing characters. A character is simply a short short int... usually 8 bits wide, or one byte. You can add, subtract, multiply and divide with chars just like you do with shorts, ints, long ints and long long ints. There's no magic...
Now let me tell you this one more time... stop trying to reopen files to do single tasks, it's a disaster waiting to happen... First consider that your output file is all upper case... what happens if the user types a lower case character? Yeah that's right, the test fails...
Look closely at where I am doing this.... (from your original code posting)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()
{
FILE *fpin, *fpout, *fprec; // declares two file streams, in and out
int tl, f, i=0;
int nl=0;
char ch, let=0; // ch is variable to be used in conversion
char input[80], output[80], record[80]; // declared arrays to store file paths
printf("Input input file name: \n");
gets(input); // input file location
printf("Input output file name: \n");
gets(output); // output file location
printf("Input record file name: \n");
gets(record); // record file location
printf("Which letter would you like the frequency of?\n");
scanf("%c", &let);
if((fpin = fopen(input,"r")) == NULL) // confirms that file has been opened
{
printf("Unable to open input file");
exit(0);
}
if((fpout = fopen(output,"w")) == NULL) // confirms that file has been created
{
printf("Unable to open output file\n");
exit(0);
}
tl=0;
while((ch=getc(fpin))!=EOF) // read input file until the end of the file
{
if (let == ch)
nl++;
if((ch>='a' && ch<='z') | (ch>='A' && ch<='Z')) // ensures that only alphabetic characters are transferred
{
putc(toupper(ch), fpout); //function to change all the characters to upper case
tl++;
}
}
fclose(fpin); // close input file
fclose(fpout);
fopen(output,"r");
if((fprec = fopen(record,"w")) == NULL) // confirms that file has been created
{
printf("Unable to open record file\n");
exit(0);
}
printf("Total number of letters in the file: %d\n", tl);
The problem here is that you are adding something new... but ignoring the chance to integrate it into your *existing* code...