Reading a txt file to check if there is word

Hello all,
Thanks in advance for your help! I am writing a program to read a file and check if there is word in it.
A part of the text file looks like this:

BF1 = max(0, P1 - 3.62627);
BF2 = max(0, 3.62627 - P1);
BF3 = max(0, PR24 - 0.673383);
BF5 = max(0, D1 - 0.677622);
BF8 = max(0, 0.330565 - F1);
BF10 = max(0, 1.43786 - P13);
BF11 = max(0, P24 - 17.8378);

I am trying to find, for Example, if P1 is there in that file. Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define number_character_raw 2000


void count_variables();

void count_variables(){

int FILE_N = 2;
FILE *fpVIS;
int P1i,P1flag;
char inptr1[number_character_raw];
for(int i = 1; i < FILE_N; i++) {
char filename[50];
sprintf(filename, "MARS%d.txt", i);
fpVIS=fopen(filename, "r");
P1flag = 0;
while(fgets(inptr1,sizeof(inptr1),fpVIS)!=NULL){

for(P1i=0;P1i<=number_character_raw;P1i++){
printf("inptr1[%d]:%c\n",P1i,inptr1[P1i]);
if((inptr1[P1i]==' ')&&(inptr1[P1i+1]=='P')&& (inptr1[P1i+2]=='1')&&(inptr1[P1i+3]==' ')){
P1i=number_character_raw;
P1flag = 1;
}
}
printf("%d\n",P1flag);
fclose(fpVIS);
}
}

}

main(){
count_variables();
}

I expect P1flag to be 1 but it turns out to be 0. Your help is greatly appreciated.
Last edited on
closed account (48T7M4Gy)
Please enclose with code tags ( select your program text and press <> on the right )

Indent your code and make it so people can read it.

Where in your code have you tried to carry out the search? Do you have a pseudocode plan to perform the search?

Also why so many #includes? You've just about thrown the whole warehouse at the search.
Why are you using FILE pointers, character arrays, sprintf(), and fgets() in a C++ program. You really should be using std::string, stringstream, C++ streams.

Also please use code tags when posting code.



The program's main purpose is to check whether there is a word in a certain file, not just a certain word like "P1"

1
2
3
4
FILE *pFile; // File handle 
char c; // Where every character will be loaded from a file via fread() - use fread() instead
char strBuf[number_character_raw]; // A place to store the "potential" word if found
bool P1flag = false; // May not be needed in future, for debugging purpose only 


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
using namespace std;
void count_variables()
{
    char c;
    char strBuf[number_character_raw];
    char filename[256];
    int FILE_N = 2;
    FILE *pFile = NULL;
    bool P1flag = false;
    bool hasWord = false;

    for(int i = 1, j = 0; i < FILE_N; ++i)
     {
          if(P1flag == true) break;

         if(pFile)fclose(pFile);
         sprintf(filename, "MARS%d.txt", i);
         pFile = fopen(filename, "r");

         if(pFile == NULL)
         {
               cout << "Error : Failed to open file \"" << filename << "\"" << endl << endl;
               break;
         }
  
         while(fread(&c, sizeof(char), 1, pFile))
         {
             if(isalpha(c))
             {
                 j = 0;
                 strBuf[0] = 0;
                 hasWord = true;
                 do
                 {
                     if(!isalnum(c)) break;
                     strBuf[j] = c; ++j;
                 } while(fread(&c, sizeof(char), 1, pFile));
                 strBuf[j] = 0;

                 cout << "(" << filename << ")" << endl;
                 cout << "    Word encountered : \"" << strBuf << "\"";
                 cout << endl << endl;
                 if(stricmp(strBuf, "P1") == 0) 
                 {
                     P1flag = true;
                     break;
                 }
             }
        }
    }
       
     if(P1flag)cout << "Hooray!! The word 'P1' has been found!!" << endl << endl;      

     if(pFile)fclose(pFile);
     if(hasWord)
     {
          cout << "The program has found (at least) one word in the file(s)" << endl << endl;
     }
     else
     {
          cout << "Sorry, the program didn't encounter any word in the file(s)" << endl << endl;
     }

     cout << "Function ended. Please press something to exit. . .";
     cin.get();
}


You might want to google if you have something (function) you don't really understand

Also try to learn and get used to writing more like (cplusplus)-style code, like others say.
Topic archived. No new replies allowed.