Extracting multiple lines into one string

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
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string combine;
    string input;

    ifstream dataFile("sequence.txt");

    if (dataFile)
        while (getline(dataFile, input))
            combine += input;

    cout << combine << endl;

    return 0;
}

Last edited on
If you expand this logic further, it will suit your needs.

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void concatenateString(char szTarget[], const char szSource[])
{
    int nT;
    for(nT = 0; szTarget[nT] != '\0'; nT++)
    {
    }
    for(int nS = 0; szSource[nS] != '\0'; nT++, nS++)
    {
        szTarget[nT] = szSource[nS];
    }

    szTarget[nT] = '\0';
}

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "This program accepts two strings\n"
    << "from the keyboard and outputs them\n"
    << "concatenated together.\n" << endl;

    cout << "Enter first string: ";
    char szString1[256];
    cin.getline(szString1, 256);

    cout << "Enter the second string: ";
    char szString2[256];
    cin.getline(szString2, 256);

    cout << "Concatenate first string onto the second"
    << endl;
    concatenateString(szString1, szString2);

    cout << "Result: <" << szString1 << ">" << endl;

    system("PAUSE");
    return 0;
}
@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/
oh, that's just the name of my function I called. concataneteString() my own function.

1
2
3
4
5
6
7
8
9
10
11
12
13
oid concatenateString(char szTarget[], const char szSource[])
{
    int nT;
    for(nT = 0; szTarget[nT] != '\0'; nT++)
    {
    }
    for(int nS = 0; szSource[nS] != '\0'; nT++, nS++)
    {
        szTarget[nT] = szSource[nS];
    }

    szTarget[nT] = '\0';
}


Well, it still works
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.
kewl
Topic archived. No new replies allowed.