DATA File Handling

I use Turbo C++. I m studying classic C++.I made the follwing program to calculate the occurance of the Word He in the File STORY.TXT. But I feel my Bold Part of the program is wrong. How can i obtain a string using (first using getline() then using get) function and compare it??I dont want to use strcmp() function...


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
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
void count();
fstream file("STORY.TXT",ios::in|ios::out);
file<<"He is playing in the ground. She\nis playinbg with her dolls.\n";
file.close();
count();
getch();
}
void count()
{
ifstream file("STORY.TXT");
file.seekg(0);int count=0;
while(!file.eof())
{
char line[10];
file.getline(line,10,' ');
cout<<line<<"\n";
if(line=="He")
++count;
}
cout<<count;
file.close();
}


The output of cout<<line<<"/n" is coming correct but value of count is coming zero...
Last edited on
typo in line 9 "playinbg" ;)

line 23: line == He checks if the whole line says He, you want to check if it the word He simply shows up in this line. since you dont want to use string-compare, you have to use a loop. run through all the characters in line and see if there is a "H". if that's the case, check if the next character is "e" and you're done.
ANd you shouldnt use == with c-strings. Use strcmp() from <cstring> or think about using std::string from <string> which come with neat member function and support for most operators.
thanx everybody
Topic archived. No new replies allowed.