cout << "Hello world!" << endl;
//Pointer to the file we're reading from (required for getc() function, which takes a pointer to its target).
FILE * pFile;
//Create a buffer to store each word parsed by the program...
//stores the current character getchar() is inspecting.
//The word the user is searching for in the file.
//for handling incremental loops...
char *fileName = "Example.txt";
//Simply tracks how many characters into the array and document we've parsed.
int wordCount = 0;
pFile = fopen (fileName, "r");
if (pFile == NULL) cout << "Error opening file " << fileName << " " << endl;
character = getc(pFile);
while((character != EOF) && (character != ' '))
for(int i=0; i < 100; i++)
character = buffer[i];
cout << buffer << endl;
Your code doesn't match the title of your post.
The title says you want to find a word in a file, your code counts characters. Thanks cire for pointing out my error.
Also why this hotchpotch of old C and C++ ?
The title says you want to find a word in a file, your code counts characters.
That's not true. All his code does is engender undefined behavior. There is not even an attempt to count characters.
And with that insight:
Could anyone help correct this code rather than suggesting a totally new way to do this?
Fixing it would be doing it in a new way, albeit you may keep some of the old variables. The first thing you should do, if you want to keep the code as close as possible to its current state, is to brush up on the concept of C strings and how you indicate the end of one.