tags:

views:

341

answers:

3
+2  A: 

Well, this is not very idiomatic C++, you might get nice results if you switched to using std::string instead of a plain array of char's

This would probably fix the problem.

Which is that the list of numbers is more that 1000 characters long, and so does not fit in your buffer.

TokenMacGuy
+3  A: 

Two problems with your code.

First, it looks like you're not accounting for newlines in your maxNum buffer size, so it stops reading either 19 or 38 characters before the end of the text file (depending on if you're using Unix-style or Windows-style line breaks). Either increase the value of maxNum accordingly, or remove the line breaks from your text file.

Second, since you're using a char array instead of a std::string to hold the buffer, it needs to be null-terminated to display properly if you use the stream operator. Add the following line after you read the buffer in (you'll also need to increase your buffer size by one to account for the extra character).

buffer[maxNum-1] = '\0';

Alternatively, you can use cout.write() to display a known-length buffer that's not null-terminated, as follows:

cout.write(buffer, maxNum);
goldPseudo
For some reason, the text is 1038 characters long and I can't understand why. I counted 20 lines of 50 characters each, and considering 19 new lines, there is double the amount of something I can't put my finger on.
L1th1um
@L1th1um: Unix-style text files use a single character (LF) as a line break. Windows-style text files use two characters (CR/LF) as a line break. Depending on what you use to create the text file, the exact character count may vary.
goldPseudo
A: 

I also recommend using C++ storage objects such as std::string s or std::vector<char> v.

With the vector<char> v you could use istream_iterator<char> ifi(infile), and std::copy(ifi, istream_iterator<char>(), back_inserter(v))

Zan Lynx