Searching for a string in a .txt file

Below is my code:

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
 #include <iostream>
#include <string>
#include <fstream>
#define SignatureBlock "~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\nNicholas Whitmore\nnicholas.whitmore@maine.edu\nCIS 215, C++ Application Programming\n~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\n"
using namespace std;

int main()
{
    string gene;
    int i = 0;
    int sum = 0;
    cout << SignatureBlock << endl;//implement signature block

    cout << "\nEnter the name of the file that contains the genome including path:\n" << endl;//prompt for file path
	            string filePath; //file path variable
	            getline(cin, filePath); //user input
                ifstream myfile(filePath.c_str());//opens file at filePath
                string genome;
                if (myfile.is_open())//check file path
                {
                   while (getline(myfile,genome))
                   {
                         cout<<"Data is: "<< genome << endl;//output the data within the file
                   }
                   myfile.close();//close the file
                }
                else cout << "Unable to open file" << endl;//if file path is incorrect
        cout << "Genes are as follows: " <<endl;

        //for the beginning of the string to the end
		for(i = genome.find("ATG",0); i!=string::npos; i=genome.find("ATG",i))
		{   
		    sum++;
            i++;
        }//end for
        cout<<i<<endl;
        cout<<sum<<endl;
      cin.get();
	  return 0;  
	    
}//end main 


Below is the output that I am getting:
~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~
Nicholas Whitmore
nicholas.whitmore@maine.edu
CIS 215, C++ Application Programming
~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~


Enter the name of the file that contains the genome including path:

C:\Users\Whitmore\Desktop\UMA work\CIS 215\MODULE 4\Content\dna_sample.txt
Data is: ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCG
Data is: CTGCCCTGCCCCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGG
Data is: CAGGAATAAGGAAAAGCAGCCTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCC
Data is: AGGCCAGTGCCGGGCCCCTCATAGGAGAGGAAGCTCGGGAGGTGGCCAGGCGGCAGGAAG
Data is: GCGCACCCCCCCAGCAATCCGCGCGCCGGGACAGAATGCCCTGCAGGAACTTCTTCTGGA
Data is: AGACCTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAGTTTAATTACA
Data is: GACCTGAA
Genes are as follows:
-1
0


First off, please ignore the
Genes are as follows:
part, my code if far from being complete. My question is, where is my error that is giving me 0instead of 5I feel that it is because of the \n white space within the .txt file. As you can see from the code, I am trying to find all instances of "ATG" within the dna_sample.txt file. Thanks in advance :)

UPDATE: If I remove all of the \n white space, I get the answer I need. unfortunately, I must use the file the way it is and not the way I edited it to be. This means that the white space is screwing things up. Help please!
Last edited on
At line 27, genome equals "GACCTGAA". This is because for every line, you overwrite genome.

I would create another string for reading each line, and for each line, append that string to genome.
YUP! There is my problem! Thanks a ton!
Why does i == -1? I don't get that one
-1 is the int value of std::string::npos. This is what std::string::find() will return if the string is not found.

That's because you write the first line to genome, then write the second line to genome (first line is lost), then write the third line to genome (second line is lost)... until you write the last line to genome (all other lines have been lost).

You might want to do it this way:
1
2
3
4
5
6
std::string line;
while (getline(myfile,line))
{
    cout<<"Data is: "<< line << endl;//output the data within the file
    genome += line; // append the new data to our genome.
}
Topic archived. No new replies allowed.