| syintel87 (1) | |
|
My input file loos like (20 lines in total) GCGAGTTCCCCCATGCGTGTCGGCCCCCGCGTCGCTTTTACTATATCCACCCCATTCCAT TGAAGGTATTTACTTGGGATCAAATACCGACTAGAGTTATTAAATCTTAATGACCTATCC TAAGTTTAAGTAACCCAGGTACAGTCGTTAGCTTAGTTCCAAGCGTCCCTCACGTGCACT ACGGTATTCCCCCTCGTCATGTGCAACCCCCCCGCTCAGCGGATTTCCAGCCGGCCAACA GTTCGACGAGAAGGCATACCCGCCAGGCAACCTACCGGCCACTCCCTGCGCCCGGACCTT What I want to make looks like GCGAGTTCCCCCATGCGTGTCGGCCCCCGCGTCGCTTTTACTATATCCACCCCATTCCATTGAAGGTATTTACTTGGGATCAAATACCGACTAGAGTTATTAAATCTTAATGACCTATCCTAAGTTTAAGTAACCCAGGTACAGTCGTTAGCTTAGTTCCAAGCGTCCCTCACGTGCACTACGGTATTCCCCCTCGTCATGTGCAACCCCCCCGCTCAGCGGATTTCCAGCCGGCCAACAGTTCGACGAGAAGGCATACCCGCCAGGCAACCTACCGGCCACTCCCTGCGCCCGGACCTT In other words, I want to extract multiple lines from text file into a single string. The below is what I have tried. But it does not work. Would you please give me a tip about my probelm? Thank you. #include <iostream> #include <fstream> #include <cstring> #include <sstream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cctype> #include <algorithm> #include <cassert> #include <iomanip> #include <stdexcept> #include <vector> #include <memory> #include <cctype> #include <iterator> using namespace std; int main() { string input[20]; fstream dataFile( "sequence.txt" , ios::in ); if( dataFile ) { for( int i=0; i<20; i++ ) { getline( dataFile, input[i], '\n' ); } } string combine; for( int i=0; i<20; i++ ) combine.append( input[i] ); cout << combine << endl; dataFile.close(); return 0; } | |
|
Last edited on
|
|
| Chervil (1206) | |||
|
Well, the getline will fail when the end of file is reached, but the for loop continues without checking. Having said that, it seems to work, providing the input doesn't have more lines than the array. A suggestion:
| |||
|
Last edited on
|
|||
| greenleaf800073 (88) | |||
If you expand this logic further, it will suit your needs.
| |||
|
|
|||
| Chervil (1206) | |
|
@greenleaf800073 I may have missed something, sorry if that's the case. But I wondered why you used function concatenateString() rather than the library function strcat()http://www.cplusplus.com/reference/cstring/strcat/ | |
|
|
|
| greenleaf800073 (88) | |||
oh, that's just the name of my function I called. concataneteString() my own function.
Well, it still works | |||
|
|
|||
| Chervil (1206) | |
|
Sure, but why re-invent the wheel, that's extra work coding, testing, debugging, when there's a fully tested library function available? The only good reason I can think of is to add extra functionality or overcome some defect of the library function. That's why I asked, I was trying to figure out the what the advantage was. | |
|
|
|
| greenleaf800073 (88) | |
| kewl | |
|
|
|