I've done a little program which reads in info from a text file, but there seems to be a problem when I try to output certain numbers.
Code for the program in question:
The script it reads from (a text file called script):Code:#include <iostream> #include <conio> #include <vector> #include <fstream> using namespace std; namespace Myprog { namespace Globals{}; class Example { unsigned int counter; public: Example(unsigned int val) { counter = val; } unsigned int get_val() { return counter; } }; namespace Globals { vector <Example *> objects; ifstream in; } namespace Script_Processing { bool make_example(); void read_script() { Globals::in.open("script.txt", ios::in | ios::binary); // The binary flag doesn't seem to make a difference. if(!Globals::in) { cout << "Unable to open script file.\n"; exit(1); } string hold_read; // Holds the string currently being tested. for( ; ; ) { while(isspace(Globals::in.peek())) Globals::in.get(); Globals::in >> hold_read.begin(); if(!strcmp(hold_read.begin(), "MakeExample")) { cout << "About to call make_example()\n"; if(Script_Processing::make_example()) return; } if(Globals::in.eof()) { cout << "Error processing script.\n"; exit(1); } } } bool make_example() { cout << "Inside make_example()\n"; string hold_read1; string hold_read2; for( ; ; ) { while(isspace(Globals::in.peek())) Globals::in.get(); if(Globals::in.peek() == '{') { Globals::in.get(); continue; } else break; } Globals::in >> hold_read1.begin(); if(!strcmp(hold_read1.begin(), "IntVar")) { while(isspace(Globals::in.peek())) Globals::in.get(); Globals::in >> hold_read2.begin(); Globals::objects.push_back(new Example(atoi(hold_read2.begin()))); return true; } return false; } } // End of namespace Script_Processing; int main() { Script_Processing::read_script(); cout << "Val in object: " << Globals::objects[0]->get_val() << '\n'; cout << "Not stuck in an infinite loop!\n"; delete Globals::objects[0]; getch(); return 0; } } // End of namespace myprog;
MakeExample {
IntVar 196010
}
The problem is that when the first number is either 1 or 0, the value output has a number of commas which seem to be related to the number of leading 1s or 0s (except when the number is 10, 100 etc in which case there are a lot of commas). I'm wondering if the problem is somewhere other than the atoi() call though, because this other little test program seems to work OK:
#include <iostream>
#include <conio>
using namespace std;
Any suggestions?Code:int main() { char *text = "196010"; int val = atoi(text); cout << val << '\n'; getch(); return 0; }



LinkBack URL
About LinkBacks


