I've created a C program which reads in a character one by one and determines if it is a vowel, numeric, punctuation etc.
The user can choose to input the characters via the keyboard or from a text file.
The first part works fine, and does everything as expected, however when I run the program and select 'read from file' choice, it simply prints out the statement in the 'else if' and completely fails to read in the text file.
I have the text file saved in the 'Source Files' folder in the Visual Studio 2015 directory, and my code is as follows (the problem area in question is close to the bottom):
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
FILE *pInputFile;
FILE *pOutputFile;
int answer = 0;
int chr = 0;
int numLowerCaseVowels = 0;
int numUpperCaseVowels = 0;
int numPunctuationChars = 0;
int numNumericals = 0;
int *pCharPointer;
int previousValue = 0;
int main()
{
// ##### KEYOBARD OR FILE INPUT #####
printf("\nKEYBOARD OR FILE INPUT?\n");
printf("Press 'k' for keyboard, 'f' for file.\n");
answer = _getch();
if (answer == 'k')
{
printf("You selected keyboard.\n\n");
printf("Enter a character.\n");
// ##### KEYBOARD INPUT #####
while ((chr = _getch()) != '\r')
{
pCharPointer = &chr;
printf("%c", chr);
// Determine if input is lower/upper case vowel.
switch (chr)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf(" Lowercase Vowel.");
numLowerCaseVowels++;
break;
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf(" Uppercase Vowel.");
numUpperCaseVowels++;
break;
default:
break;
}
//Determine if char is punctuation.
if (_istpunct(chr))
{
printf(" Punctuation Character.");
numPunctuationChars++;
}
//Determine if char is numeric.
if (iswdigit(chr))
{
printf(" Numeric Character.");
numNumericals++;
}
//Determine if char is duplicate character.
if (previousValue == chr)
{
printf(" Duplicate Character.");
previousValue = *pCharPointer;
}
else
{
previousValue = *pCharPointer;
}
printf("\n");
}
}
// ##### PROBLEM AREA #####
else if (answer == 'f')
{
printf("You've chosen file.\n\n");
fopen_s(&pInputFile, "Text.txt", "r");
chr = getc(pInputFile);
while ((chr = getc(pInputFile)) != EOF)
{
printf("%c", chr);
}
fclose(pInputFile);
}
else
{
printf("Invalid Input. Exiting...\n");
exit(1);
}
// ##### ANALYSIS #####
printf("\nNumber of lowercase vowels: %d\n", numLowerCaseVowels);
printf("Number of uppercase vowels: %d\n", numUpperCaseVowels);
printf("Number of punctuation characters: %d\n", numPunctuationChars);
printf("Number of lowercase vowels: %d\n\n", numNumericals);
return 0;
}
Sorry for the amount of code, I thought it would be best to include the entire source code.
It doesn't actually perform any analysis on the characters from the text file yet, I'm simply trying to get to the stage where it will actually read the file first. Any help would be appreciated.
The problem you'll have even if the file is found is that you lose the first char of your input.
Correct way of doing it:
errno_t err;
if ((err=fopen_s(&pInputFile, "Text.txt", "r"))==0)
{
// file exists: don't read a char before the loop or
// it will be lost
while ((chr = getc(pInputFile)) != EOF)
{
printf("%c", chr);
}
fclose(pInputFile);
}
else
{
fprintf(stderr,"Cannot open file, error %d\n",err);
// handle the error further if needed
}