Can't compare strings..

I am trying to write a script which reads some data from a file and builds a class instance, but for some reason I can't read the strings properly:

1
2
3
4
5
6
7
if (type_string == "OUT") return Codon(&Codon::OUT);
  else 
  {
  // invalid string received
  std::cout << "ERROR: Unknown string: \"" << type_string << "\"";
  throw;
  }


The output is:

ERROR: Unknown codon function string: "OUT"


Yet "OUT" is exactly the same as the type_string compare (using strcmp(type_string,"OUT") produces the same result. Additionally, when I view the "type_string" char array in VS, it simple says it contains "OUT". What could be causing this and is there a reliable way of comparing strings?
Please show how type_string was defined and how you tried to read the file for this variable.

Here is the text file it's reading:

# Simple act gene which outputs the first memory vector value it receives

OUT #output memory value


And here is the script:

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
// build a prototype codon sequence from a file array as input
std::vector<Codon> BuildCodonSequence(char const filename[])
{
	unsigned static const large_int = 1000000; // maximum characters in a line of strings before this will break

	std::vector<Codon> codon_vector; // the codon vector that will be returned

	std::ifstream file;
	file.open(filename); // open file

	while(file.good()) 
	{
		switch (file.peek())
		{
		case '#' :
			// found a comment tag; ignore the rest of the line
			file.ignore(large_int,'\n');
			break;
		case ',' :
			// found a delimiter; ignore it
			file.ignore(1,EOF);
			break;
		case '\n' :
			// found end of line, ignore it
			file.ignore(1,'\n');
			break;
		case '\t' :
			// found a tab, ignore it
			file.ignore(1,'\t');
			break;
		default:
			// found something else, presumably a codon function string

			// create a string container and fill it with the codon type string
			char codon_type_string[3];
			file.get(codon_type_string,4);

			// add the type to the codon vector
			codon_vector.push_back(Type(codon_type_string));
			break;
		}
	}
	return codon_vector;
}


Where Type() is the function which is given type_string as in the original post.
closed account (Dy7SLyTq)
i believe this is your error
http://www.cplusplus.com/reference/istream/istream/get/
Can you explain?
Topic archived. No new replies allowed.