File Input Streams
Hey, folks. I've written a program for my conlanging project which takes a file full of words in the conlang, inputs them into a null-terminated character array one at a time, and then syllabifies them and runs statistical analyses to see if I'm keeping the letter frequencies constant. Anyway, it all works except for the file inputting function:
I need some kind of boolean statement to substitute in for the ?????s that will tell the function to stop reading the file when it gets to the end. I've tried currentWord[0] != '\0' and currentWord[0] != EOF and currentWord[0] != (char)EOF and all them result in an infinite loop whereby the function keeps reading in the last word over and over.
I know that cin >> regularly has infinite-loop conniption fits when you tell it you're inputting a number, and give it a character instead. But I thought that inputting something as a character wouldn't cause any problems? I think I remember seeing somewhere that the value of EOF is -1, which might explain why it's having issues. Should I be using getline instead?
As a temporary fix, I've set the text to currentWord[0] != 'q' and manually inserted a "q" at the end of the file. I suppose I could always declare an ofstream for adding "q" to the end of any file I want to process, but that seems bizarre.
void fileInput (char filename[])
{
char currentWord[100] = {'\0'};
strcat (filename, ".txt");
ifstream file (filename, ios::in);
do {
file >> currentWord;
cout << currentWord << endl;
if (????????????)
analyzeWord (currentWord);
} while (?????????????);
}I need some kind of boolean statement to substitute in for the ?????s that will tell the function to stop reading the file when it gets to the end. I've tried currentWord[0] != '\0' and currentWord[0] != EOF and currentWord[0] != (char)EOF and all them result in an infinite loop whereby the function keeps reading in the last word over and over.
I know that cin >> regularly has infinite-loop conniption fits when you tell it you're inputting a number, and give it a character instead. But I thought that inputting something as a character wouldn't cause any problems? I think I remember seeing somewhere that the value of EOF is -1, which might explain why it's having issues. Should I be using getline instead?
As a temporary fix, I've set the text to currentWord[0] != 'q' and manually inserted a "q" at the end of the file. I suppose I could always declare an ofstream for adding "q" to the end of any file I want to process, but that seems bizarre.
