What's wrong with this code? (Find word in .txt file)

Trying to write some code to parse a .txt file for a searched-for word.

Could anyone help correct this code rather than suggesting a totally new way to do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include <iostream>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include <stdio.h>



using namespace std;

int main()
{
    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...
    char buffer[100];

    //stores the current character getchar() is inspecting.
    int character;

    //The word the user is searching for in the file.
    string searchTerm;

    //for handling incremental loops...
    int i;

    char *fileName = "Example.txt";




    //Simply tracks how many characters into the array and document we've parsed.
    int charCount;
    int wordCount = 0;





    pFile = fopen (fileName, "r");


    if (pFile == NULL) cout << "Error opening file " << fileName << " " << endl;
    else
    {
        character = getc(pFile);

        while((character != EOF) && (character != ' '))
        {
         for(int i=0; i < 100; i++)
         {
             character = buffer[i];

         }

        }

        cout << buffer << endl;


    }




So far it just gives me a blinking cursor..
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++ ?

A simple demo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
  const string filename = "Your file name";
  ifstream src(filename);
  if (!src)
  {
    perror("File error: ");
    return errno;
  }
  string searchTerm, input;
  while (src >> input)
  {
    // TO DO
    // strip non-alpha chars from input
    // check if searchTerm is == input
  }
  cout << "Good bye.";
}
Last edited on
Your code doesn't match the title of your post.

That's true.


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.
Topic archived. No new replies allowed.