C++/C++ file handling
Expert: vijayan - 11/17/2009
Questionhello sir i m trying to search a string(i.e keyword) stored in one file with the program stored into another file .But the probelm is that i want to read from file (i.e is the file of the program)line by line i.e is first line should get me line #include<stdio.h> only.So please tell me how it can happen in c++
how can we read line line from any file
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<string.h>
#include<dos.h>
fstream fin,filein;
int match(char *,char *);
void main()
{ clrscr();
char ch,a[50],b[50][50],temp[20],kwrd[10];
int i=0,j=0,tmp,flag=0,max=0,c;
fin.open("demoprg.txt",ios::in);
filein.open("tmpprg.txt",ios::out);
if(!fin ||!filein)
{ cout<<endl<<"File not exists...";
getch();
exit(0);
}
cout<<endl<<"\t\t****PROGRAM IS ******"<<endl;
while(!fin.eof())
{ fin.get(ch);
cout<<ch;
filein.put(ch);
}
cout<<endl<<"File has been copied.....;";
fin.close();
filein.close();
getch();
clrscr();
filein.open("keywords.txt",ios::in);
if(!filein)
{ cout<<endl<<"file not exists......:";
getch();
exit(0);
}
while(!filein.eof())
{ filein.get(ch);
// delay(100);
if(ch!=' ')
{ b[i][j]=ch;
cout<<b[i][j];
flag=0;
if(max<j)
max=j;
j++;
}
else
{ if(ch==' ' && flag==0)
{ b[i][j]='\0';
tmp=i;
flag=1;
i++;
j=0;
}
}
}
cout<<"********\n";
cout<<max<<endl;
for(i=0;i<tmp+1;i++)
{ for(j=0;j<max+1;j++)
{ cout<<b[i][j]; }
}
filein.close();
cout<<endl<<"***Keywords ****";
fin.open("tmpprg.txt",ios::in);
if(!fin)
{ cout<<endl<<"file not exists...:";
getch();
exit(0);
}
i=0;
j=0;
int k=0;
getch();
clrscr();
while(k<tmp+1)
{ i=k;
cout<<endl<<i<<" keyword->"<<b[i];
while(!fin.eof())
{ fin.get(ch);
while(1)
{ a[j]=ch;
j++;
fin.get(ch);
if(ch==' ')
break;
}
a[j]='\0';
cout<<"a[] ->"<<a;
getch();
j=0;
if(strcmp(a,b[i])==0)
cout<<endl<<"Keyword found ->"<<b[i];
}
fin.seekg(0);
k++;
}
getch();
exit(0);
}
int match(char *body,char *search)
{ int chk;
int len =strlen(body);
int len2 =strlen(search);
for(int p=0;p<len;p++)
{ if(body[p]==search[0])
{ chk=1;
for(int q=0;q<len2;q++)
{ if(body[p+q]==search[q])
chk=1;
else
chk=0;
}
}
if(chk)
return 1;
}
return 0;
}
AnswerThese are not standard c++ headers
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<string.h>
#include<dos.h>
<conio.h> and <dos.h> are not, and never have been standard C headers; functions like clrscr() are not part of any standard C or C++ header. It is best to avoid their use.
The C++ headers that need to be included are
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<cstring>
If you do not want to qualify ever use of a library provided identifier with std::, you may also add
using namespace std ;
It is preferable to use a std::string instead of c-style char arrays - they are easier to use and grow and shrink as needed. see:
http://www.cprogramming.com/tutorial/string.html
To read a single line from an input stream into a string, use std::getline.
see:
http://www.cplusplus.com/reference/string/getline/
To read input from an input stream till eof, do not use a construct like:
while( !filein.eof() )
{
filein.get(ch);
// process ch which has just been read
// ...
}
This has a subtle error: eof() is checked before the attempt to read, you need to check for eof() or any other error after you had tried to perform an input operation.
The fix is easy; just write
while( filein.get(ch) )
{
// process ch which has just been read
// ...
}
An example of reading a file line by line and printing it out to stdout with line numbers:
#include <string>
#include <fstream>
#include <iostream>
int main()
{
const char* file_name = ""keywords.txt" ;
std::ifstream file( file_name ) ; // open the file
if( !file ) { /* error file could not be opened */ }
std::string line ;
int line_number = 0 ;
while( std::getline( file, line ) )
std::cout << ++line_number << '.' << line << '\n' ;
}
You could use the find member of std::string to search for the keyword.
see:
http://www.cplusplus.com/reference/string/string/find/