char array input from text file

The goal of this program is to read lines from a text file that contain infix expressions ending with a ';' character and output them as postfix expressions in either another file, or to the screen if not output file is given. I cannot use the STL string class, I can only use my own class I created called String. After finding one way to get this to work using getline and then sending the char array from that to my String, the String output is wrong. This is my main file:

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
#include<iostream>
#include<fstream>
#include"string.h"
#include"stack.h"

int main(int argc, char *argv[]){
	if (argc < 2) {
        std::cerr << "Error: One input file is required." << std::endl;
        std::cerr << "  For example: file1.txt" << std::endl << std::endl;
        return(1);
    }
	if (argc > 3) {
        std::cerr << "Error: Too many files. One input and one output allowed" << std::endl;
        std::cerr << "  For example: file1.txt file2.txt" << std::endl << std::endl;
        return(1);
    }

	char line[100];

	//Open file and read lines into string object
	std::ifstream in(argv[1]);
	//Check that file was successfully opened
	if(!in){
		std::cerr<<"Couldn't open " << argv[1] <<", exiting. \n";
		exit(1);
	}
	else{
			std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
			std::cin.rdbuf(in.rdbuf()); //redirect std::cin to ifstream in
			std::cin.getline(line, 100, ';'); //read from file to string object
			std::cout << std::cin.gcount()<<std::endl;
			std::cin.rdbuf(cinbuf); //reset to standard input
	}
	in.close(); //close input file
	String s1(line);

	//If output file was specified, put output in it. Else output to screen.
	if(argc == 3){
		std::ofstream out(argv[2]);
		if(out.is_open()){
			out << s1;
			out.close();
		}
	}
	else {
		std::cout << s1 << std::endl;
		std::cout << s1.length()<<std::endl;
	}
	return 0;
}



Everything compiles and runs fine, but when I output my String object I get this:

( AX + ( B * C ) ) åÿÿÿÿ Ð ÿÿÿÿÿÿÿÿ¬Ð àÙ

I'm guessing that all of the nonsense is from the empty elements in my char array? My constructor checks for '\0' to be the end of the array, it should end with that character right?

My String class includes a length function to output the String's capacity integer value. Somehow my String object has a length of 172.

This is my constructor if it helps:

1
2
3
4
5
6
7
8
9
10
11
String::String(const char str[]){
	int i=0;
	capacity = 1;
	for(int s=0; str[s]!='\0'; ++s) capacity+=s;
	ptr=new char[capacity+1];
	while((str[i] != '\0')&&(i<=capacity)){
		ptr[i] = str[i];
		++i;
	}
	ptr[capacity+1]='\0';
}


This is for a class project and that is why I can't use string. My next lab later this week I will be talking to the instructor about this, but I don't know what to do until then. Any help at all would be greatly appreciated!
Topic archived. No new replies allowed.