I got a problem in infile and outfile

I was trying to write a program that search and count one or more specific char(s)
but I m not getting what I exactly wanted
here is the program\

// program to read "*" ,"**" and "***" from a txt. file
#include<iostream>
using namespace std;

#include<fstream> //flie stream
using std::ifstream; //input file stream

#include<cstdlib> //exit function
#include<iomanip> // for setw and setprecision
#include<string>
int i=1;
//displays each single record from file
void outputline(char alarm)
{
if(alarm=='*')
{
cout<<i<<"-"<<alarm<<endl;
}
}

int main()
{
//opens the file
ifstream inClientFile("sample file.txt",ios::in);

//exit program if ifstream could not open file
if( !inClientFile )
{
cerr<<"File cannot be openned "<<endl;
exit(1);
}

//Otherwise
char alarm;



//display each record in file

while (inClientFile>>alarm)
{
outputline(alarm='*');
i++;
}
return 0;
}


Please guide me
God bless u all
thank you
1
2
3
4
5
while (inClientFile>>alarm)
  {
  outputline( alarm );
  i++;
  }
Please see the modified portion, I have included the explanation in the comments

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
68
// program to read "*" ,"**" and "***" from a txt. file
#include<iostream>
using namespace std;

#include<fstream> //flie stream 
using std::ifstream; //input file stream

#include<cstdlib> //exit function 
#include<iomanip> // for setw and setprecision 
#include<string> 

int i=1;
//displays each single record from file
void outputline(char alarm)
{
	
	if(alarm=='*')
	{
	cout<<i<<"-"<<alarm<<endl;
	}
} 

int main()
{
//opens the file
ifstream inClientFile("sample file.txt",ios::in);

//exit program if ifstream could not open file
	if( !inClientFile.is_open() ) //?? check it with the is_open() function
	{
		cerr<<"File cannot be openned "<<endl;
		exit(1);
	}

//Otherwise
char alarm;
//@@@@@ modified from here
std::string line; // ?? to hold a line 
size_t position_found; // ?? to hold character positions
int line_number=1;	// ?? to hold the line number
int j=0; // ?? a variable to feed differentt starting positions during the search
//display each record in file 
	
	while ( !inClientFile.eof() ) // ?? check it with eof() function (eof means end of file
	{
		getline(inClientFile,line); // ?? get one line in the string line
		// we can use string class methods to find a character
		position_found=0;
		while(position_found != std::string::npos){
			position_found=line.find('*',j);// do the search for the entire one line starting from postion 0
			if(position_found != std::string::npos)
				std::cout<< " In line number " << line_number << " found " << " * at position " <<(int) position_found << "\n";
			j=position_found+1;	// change j to the next position so that in the next step it starts after we found first *
		}
		line_number++;

		//?? I'm commenting it out to get the display clearly, you can modify it and reuse it, 
		//before that please understand what I have done to search for a character and printing it
		// Hope this is useful, cheers Anil
		//outputline(alarm='*');
		//i++;
	}
//@@@ modified END

return 0;
}

Thank u Sir
I had my internet link down for days and was unable to get here

thanks again
Topic archived. No new replies allowed.