BinaryReader -> BinaryWriter

Hi all. I'm making a program with many variables read from a file. It is a large project for me. Anyway, I wrote a program that converts a basic BinaryReader function into a BinaryWriter function, and thought I would share.

There are a few places where things get a little messed up, but certainly fixable by hand (dealing with strings for example). Also, I assume you can do things like copy/paste your code into a file named "input.txt" and the like.
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <fstream>
#include <string>

int main(void)
{
	std::ifstream in("input.txt");
	std::ofstream out("output.txt");
	
	std::string line;
	std::string temp;
	size_t firstSpace;	
	size_t firstParenth;
	size_t whiteSpace;
	
	
	while (getline(in, line))
	{
		if (line.find("for (UInt32") != std::string::npos)
		{
			out << line << '\n';
			continue;
		}
		
		if (line.find(" new ") != std::string::npos)
		{
			continue;
		}
		
		whiteSpace = line.find_first_not_of("  \t");
		if (whiteSpace != std::string::npos)
			line.erase(0, whiteSpace);
		else
		{
			out << line << '\n';
			continue;
		}
		
			
		
		if (line.find(".Add") != std::string::npos
		 && line.find("new") == std::string::npos
		)
		{
			temp = line.substr(0, line.find_first_of('.'));
			line.erase(0, line.find_first_of('(') + 1);
			temp.append("[(int)i]");
			line.insert(line.find_first_of('(') + 1, temp);
			line.erase(line.find_last_of(')'), 1);
		}
		else
		{		
			firstSpace = line.find_first_of(' ');
			firstParenth = line.find_first_of('(');
			
			if (firstSpace != std::string::npos
			 && firstParenth != std::string::npos
			 )
			{
			//insert ( size_t pos1, const string& str, size_t pos2, size_t n );
			line.insert(firstParenth + 1, line, 0, firstSpace);
			firstSpace = line.find_first_of(' ', firstSpace + 1);
			
			line.erase(0, firstSpace + 1);
			}
				
			
		}
		
		if (line.length() > 1)
		{
			line.erase(0, line.find_first_of('('));
			line.insert(0, "bw.Write");
		}
		
		if (whiteSpace != std::string::npos)
			line.insert(line.begin(), whiteSpace, ' ');
			
		out << line << std::endl;
	}	
	in.close();
	out.close();
	return 0;
}


Input:
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
            u1 = br.ReadUInt32();
            resKey = br.ReadUInt32();
            inversFlg = br.ReadByte();
            repeatFlg = br.ReadByte();
            speed = br.ReadSingle();

            rotationReps = br.ReadUInt32();
            rotation = new ObservableCollection<float>();
            for (UInt32 i = 0; i < rotationReps; i++)
                rotation.Add(br.ReadSingle());

            sizeAdjReps = br.ReadUInt32();
            sizeAdj = new ObservableCollection<float>();
            for (UInt32 i =0; i < sizeAdjReps; i++)
                sizeAdj.Add(br.ReadSingle());

            alphaAdjReps = br.ReadUInt32();
            alphaAdj = new ObservableCollection<float>();
            for (UInt32 i =0; i < alphaAdjReps; i++)
                alphaAdj.Add(br.ReadSingle());

            colorAdjReps = br.ReadUInt32();
            colorAdjRed = new ObservableCollection<float>();
            colorAdjGreen = new ObservableCollection<float>();
            colorAdjBlue = new ObservableCollection<float>();
            for (UInt32 i =0; i < colorAdjReps; i++)
            {
                colorAdjRed.Add(br.ReadSingle());
                colorAdjGreen.Add(br.ReadSingle());
                colorAdjBlue.Add(br.ReadSingle());
            }

            yStretchReps = br.ReadUInt32();
            yStretch = new ObservableCollection<float>();
            for (UInt32 i =0; i < yStretchReps; i++)
                yStretch.Add(br.ReadSingle());
            
            initIntenseVar = br.ReadSingle();
            initSizeVar = br.ReadSingle();
            u2 = br.ReadSingle();
            u3 = br.ReadSingle();
            u4 = br.ReadSingle();
            u5 = br.ReadSingle();


Output:
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
            bw.Write(u1);
            bw.Write(resKey);
            bw.Write(inversFlg);
            bw.Write(repeatFlg);
            bw.Write(speed);

            bw.Write(rotationReps);
            for (UInt32 i = 0; i < rotationReps; i++)
                bw.Write(rotation[(int)i]);

            bw.Write(sizeAdjReps);
            for (UInt32 i =0; i < sizeAdjReps; i++)
                bw.Write(sizeAdj[(int)i]);

            bw.Write(alphaAdjReps);
            for (UInt32 i =0; i < alphaAdjReps; i++)
                bw.Write(alphaAdj[(int)i]);

            bw.Write(colorAdjReps);
            for (UInt32 i =0; i < colorAdjReps; i++)
            {
                bw.Write(colorAdjRed[(int)i]);
                bw.Write(colorAdjGreen[(int)i]);
                bw.Write(colorAdjBlue[(int)i]);
            }

            bw.Write(yStretchReps);
            for (UInt32 i =0; i < yStretchReps; i++)
                bw.Write(yStretch[(int)i]);
            
            bw.Write(initIntenseVar);
            bw.Write(initSizeVar);
            bw.Write(u2);
            bw.Write(u3);
            bw.Write(u4);
            bw.Write(u5);
Topic archived. No new replies allowed.