search string in a file

May 12, 2013 at 12:42am
how can i search string like "ATG" or "CTG" etc in the outfile, and when it will find "ATG" then it will show like "we find ATG in ATGCCTGAGA sequence" or else "cant find" as output.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
    fstream outputfile;
  outputfile.open("dna1.dat",ios::out);
                      outputfile<<"Reading DNA sequences from dna1.dat..."<<endl;
                      outputfile<<"ATGCCTGAGA"<<endl;
                      outputfile<<"ACCTGACA"<<endl;
                      outputfile<<"ATCCTGAC"<<endl;
                              
                      outputfile.close();
  fstream inputfile;
  
  inputfile.open("dna2.dat",ios::out);
  {
                      inputfile<<"Reading DNA sequences from dna2.dat..."<<endl;
                      inputfile<<"ATGCTGAGA"<<endl;
                      inputfile<<"ACCTGACA"<<endl;
                      inputfile<<"ATCCTAGC"<<endl;
                              
  inputfile.close();
  }
  fstream anotherfile;
  string filename;
  char ch;
  fstream outfile;
  fstream file;
  cout<<"enter";
  cin >> filename;
  file.open(filename.c_str());
  if (file)
  {
           
           file.get(ch);
           while(file)
           {
                      
                      outfile.put(toupper(ch));
                      cout<<ch;
                      file.get(ch);
           }
           file.close();
  }
  else cout<<filename<<"can't found";
 string x;

  system("pause");
  return 0;
}
May 12, 2013 at 7:18am
I haven't ever tried this, but maybe you can load the text from the files to a string and then perform a string search (string::find - http://www.cplusplus.com/reference/string/string/find/)
Topic archived. No new replies allowed.