How to read a string of a file character by character and store it within a vector c ++

0

Hello everyone, my problem is that I have a chain of this style. Every time I run my program the chain changes because it is random.

CGGACCCGTGGTCCGAGGTCTAAAGTGATTAGAAGCCGGT

The question is that I must read character by character back and forth but at the moment of reading 4 characters store them and then work with the stored chains. The problem is that when reading 4 characters for example CGGA I do not jump directly to the other 4 but I read the following 4 GGAC to store them and so on. To later order them by number of repetitions.

My iterator should be moved 1 by 1 when reading 4 characters store that string, so along the entire chain. For example, having ACGTAGTCATGC. The first chain that would store would be ACGT, the second CGTA, the third GTAG, the fourth TAGT, the fifth AGTC, as well as the entire chain.

I hope someone gives me a clue. I would appreciate it.

    
asked by Tada192 06.12.2017 в 17:57
source

1 answer

0

If you're always going to read four by four, you can use ifstream::read :

  

basic_istream& read( char_type* s, std::streamsize count );

     

Extracts characters and stores them in the memory attached to the array of characters whose first element is pointed to by s . The characters are extracted and stored until any of these conditions occurs:

     
  • count characters have been extracted and saved.
  •   
  • The end of the file is found (in which case, it will be called setstate(failbit|eofbit) ). The number of characters that could be extracted can be consulted using gcount() .
  •   

This could be a way to do it:

std::vector<std::string> cuartetos;
char cuarteto[4]{};

if (std::ifstream datos{"datos.adn"})
{
    while (datos.read(cuarteto, 4))
        cuartetos.push_back(cuarteto);
}
    
answered by 07.12.2017 в 08:38