algorithm - Reading Lines after a line in C++ not working -


i've spent 2 hours trying parse following bytes file :

>rosalind_6404 cctgcggaagatcggcactagaatagccagaaccgtttctctgaggcttccggccttccc tcccactaataattctgagg >rosalind_5959 ccatcggtagcgcatccttagtccaattaagtccctatccaggcgctccgccgaaggtct atatccatttgtcagcagacacgc >rosalind_0808 ccaccctcgtggtatggctaggcattcaggaaccggagaacgcttcagaccagcccggac tgggaacctgcgggcagtaggtggaat 

i store word rosalind_, , store every line, concatenate all, , have 1 string having lines.

i tried following code, still doesn't work probably, miss last line.

int main() {     std::ifstream infile("data_set.txt");     map < int, string > id;     map < int, string > datasetmap;     int idnumber= 0;     int iddatasetnumber = 0;      std::string line;     std::vector<string> datasetstring;     std::string seqid;      while (!infile.eof() )     {          while(std::getline(infile, line))         {              if ( line.substr(0,1)== ">")             {                  conct = "";                 seqid = line.substr(1,line.length() - 1);                    id.insert(make_pair( idnumber++, seqid));                 linenumber = 0;                 line.clear();                 std::string data= "";                 if(datasetstring.size()>0)                 {                     (int = 0; i<datasetstring.size(); i++)                     {                         data+=datasetstring[i];                     }                     datasetmap.insert(make_pair(iddatasetnumber++, data));                 }                 datasetstring.clear();             }              if(!line.empty() )             {                  datasetstring.push_back(line);             }          }      } 

i'm trying practice problems solving approaches, , gave me headache.

i'm looking better approach also.

this code want:

#include <map> #include <vector> #include <string> #include <iostream> #include <fstream>  int main() {     std::istream& infile = std::cin;     std::map < int, std::string > id;     std::map < int, std::string > datasetmap;     int idnumber= 0;     int iddatasetnumber = 0;      std::string line;     std::vector<std::string> datasetstring;     std::string seqid;      bool success = std::getline(infile, line);     while(success) {         if( line.substr(0,1) == ">" ) {             seqid = line.substr(1,line.length() - 1);             id.insert(make_pair( idnumber++, seqid));             std::string data;             while(success = std::getline(infile, line)) {                 if(line.substr(0,1) == ">") break;                 data += line;             }             datasetmap.insert(make_pair(iddatasetnumber++, data));         } else {             std::cout << "invalid input file. needs start >some_id" << std::endl;             return 1;         }     }      std::cout << "parsed data ----------------" << std::endl;     for(std::map<int,std::string>::const_iterator = datasetmap.begin(); != datasetmap.end(); ++it) {         std::cout << "id: " << id[it->first] << std::endl;         std::cout << (it->second) << std::endl;     } } 

it first reads line input file , tries parse id. if fails, returns error. reads data until finds id or eof. inserts data , continues parse id found if didn't encounter eof. working demo: http://ideone.com/f4mcrc

note: fails when file empty, might want check empty string or string containing whitespaces in else of id check , skip it.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -