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