Won't output to file

I am trying to take a string from dna file and output the rna sequence of it on the dna2 file. For example if the 1st file had:
AGTGTGCGTC
GTCGATCGCT
output on 2nd file should be:
TCACACGCAG
CAGCTAGCGA

but for some reason the output file is blank


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
44
45
46
47
48
49
50
51
  #include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
    char dna[100];
    char dna2[100];
    int size;
    ifstream infile;
    ofstream outfile;
    int length;
    infile.open("dna.txt");
    outfile.open("dna2.txt");
    
     if (infile.fail())
     { cout<<"Error File cannot be open!"<<endl;
       exit(1);
      }
    infile>>dna;
    while (!infile.eof())
    {
      
      length = strlen(dna);
      for (int i=0; i<length;i++)
      {
          if (dna[i]=='A')//Takes A and replaces with T
          {
            dna2[i]=='T';
          }
          if (dna[i]=='T')//Takes T replaces with A
          {
            dna2[i]=='A';
          }
          if (dna[i]=='G')//Takes G replaces with C
          {
            dna2[i]=='C';
          }
          if (dna[i]=='C')//Takes C replaces with G
          {
            dna2[i]=='G';
          }
       
       }
       outfile <<dna2<<endl; //supposed to output the new string on textfile
       }
    infile.close();
    outfile.close();
system ("pause");
return 0;
} 
dna2[i]=='T'; does nothing and compiler should warn you about it. You need assigment =, not comparison == here
I fixed it still does not work.
Your code should not even compile: for strlen you need to include <cstring>

Additionally line outfile <<dna2 has undefined behavior (as dna2 is not null-terminated)
Also your loo will either not execute (if there is no whitespaces/newlines in file) or will be infinite.
Never loop on .eof(). It does not work as you think.
Ok I fixed up my code a little bit, thanks for the help!! I am using Dev C++ maybe its a compiler issue. But one last problem it writes on the file but the output should look like this:
CAATCAGCTACG
CAATCAGCTACA
CAATCAGCTACT


but the output is this:
CAATCAGCTACGCAATCAGCTACACAATCAGCTACT

I know my output, outputs individual characters one at a time, but I can't think of any other ideas.

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
44
45
46
47
48
49
50
51
52
53
54
#include<fstream>
#include<iostream>
#include<string>

using namespace std;

int main ()
{
	string dna;
	string rna;
	
	int length;
	ifstream myfile;
	ofstream newfile;
	
	myfile.open("dna.txt");
	newfile.open("dna2.txt");
while (!myfile.eof())	
{   
    getline(myfile,dna);
	length = dna.length();


	for(int i = 0; i < length; i++)
	{
		if (dna[i] == 'A')
		{
			rna[i] = 'T';
		}
		if (dna[i] == 'T')
		{
			rna[i] = 'A';
		}
		if (dna[i] == 'C')
		{
			rna[i] = 'G';
		}
		if (dna[i] == 'G')
		{
			rna[i] = 'C';
		}
		newfile << rna[i];
	}
}


	myfile.close();
	newfile.close();	


	system("pause");
	return 0;

}
Output newline after you done with the string.
And you have the problem again: rna contains 0 elements, so rna[i] is invalid and can lead to crashes.
You still not fixed looping on eof problem: it is a very weak construct which can break from slightlest modification of input data or your code.
You do not need to close files manually. It is done for you in the destructor.
You should prefer to open files in constructor.
And use standard algorithms.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>

char replace(char c)
{
    if (c == 'A') return 'T';
    if (c == 'T') return 'A';
    if (c == 'G') return 'C';
    if (c == 'C') return 'G';
    return c;
}

int main()
{
    std::ifstream infile("dna.txt");
    std::ofstream outfile("dna2.txt");
    std::string dna;
    while (infile >> dna) {
        std::transform(dna.begin(), dna.end(), dna.begin(), replace);
        outfile << dna << '\n';
    }
}
Thank you so much you were such a great help! :D and I never knew eof was bad since many online examples and codes seem to have it. I will try avoiding it in future use
Topic archived. No new replies allowed.