I am attempting to read in a string of 123 characters and strip out certain fields. The string 'can' contain allot of blanks and special characters; example: A00034A16 V006 * C 136-01 0001-9999 A2160-CB2E10 1 A2160-J6 &M 922 . It appears my string input is have troubles with the blanks and ends prior to reading the entire line:
While the syntax is correct I receive an error at run time: 'std::out_of_range' what(): basic_string::substr. Some how there has to be a way of loading all 123 characters from the input string into 'myString'. What am I missing. ( I even attempting reading in a char[123] and equating it to 'myString' to no avail). Thanks for your assistance!!
By the way, when I previewed this message allot of blanks were omitted from the example line. Between A00034A16 and V006 there are actually 20 spaces.
#include <fstream>
int main()
{
std::ifstream ifs("input.txt", std::ios::binary);
char buf[123];
// try to read 123 chars, len contains the actual number read
int len = ifs.readsome(buf, 123);
// Did we read anything? (you may want to check that len == 123 here)
if(len > 0)
{
// Ok, turn it into a std::string
std::string line(buf, len);
// now examine the string...
}
return 0;
}