Ofstream not writing to file

EDIT: I fixed it. I shouldn't program so late at night. I used "&& instead of "||" for the previous while loop.


I am trying to turn a list of elements into an XML format document. Weird bug is that the things inside "while(in >= 65 && in <= 122)" doesn't output to output file and I can't figure out what. I tried flushing, that doesn't work. It's not about output buffer because if you put "ostream << "Test"; " right before the while loop, it outputs. For some reason it doesn't output to file inside the for loop.

I am wondering if the "while(in >= 65 && in <= 122)" is not detecting certain invisible characters. I copied and pasted the elements from a table online.

All I know is that the program enters the while loop, but never outputs to file. The first while loop does though.

The input and output files are regular .txt format with Unix/Linux line ending.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <ctype.h>
using namespace std;

void parse()
{
	char in;
	ifstream instream("PTparse2.txt");
	ofstream ostream("out.txt");

	bool parsedMolarMass = false;

	while(instream.good())
	{
		ostream << "<element>\n";
		
		instream.get(in);
		cout << in << endl;

		ostream << "<molarMass>";

		while(isdigit(in) || in == '.')
		{
			ostream << in;
			instream.get(in);
		}
		ostream << "</molarMass>\n";

                //FIX: actually meant "||" not "&&"
		while(in == ' ' && in =='\t')
		{
			instream.get(in);
		}
		ostream << "<name>";

                //Will write "Test", but not the things in loop.
                ostream << "Test";
                
                //doesn't output inside below while loop
		while(in >= 65 && in <= 122)
		{
			cout << "@@" << in << "@@" << endl;
			ostream << in;
			instream.get(in);
		}
		ostream << "</name>\n";

		while(in != '\n' )
		{
			instream.get(in);
		}

		ostream << "</element>\n\n\n";

	}

	ostream.close();
	instream.close();

}

int main(){
	parse();

	return 0;
}


Sample input file:
1
2
3
1.0079 Hydrogen	H	1
4.0026 Helium	He	2
6.941	Lithium	Li	3
Last edited on
Topic archived. No new replies allowed.