Input from text file into custom string object

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. The problem I'm having is that I can't get the input from the file into my String object, or even into a character array to then put into the object. 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
#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);
    }

	String s1;
	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{
             //THIS IS WHERE I NEED HELP
	     std::getline(line, in, ';'); //read from file to string object
	}
	in.close(); //close input file

	//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;
	return 0;
}


I've tried many different implementations using the >> operator and getline. I have >> overloaded for my String class. Maybe it is part of the problem? Here is the function for it:

1
2
3
4
5
6
7
std::istream& operator>>(std::istream& in, String& rhs){
	char ch[100];
	in>>ch;
	String temp(ch);
	rhs=temp;
	return in;
}


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!
I think I may have solved this problem, but not 100% sure yet. I now have this in my else statement for the input:

1
2
3
4
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::cin.rdbuf(cinbuf); //reset to standard input 


And then I create my String object with:

String s1(line)

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';
}
Last edited on
Topic archived. No new replies allowed.