Wednesday, 15 January 2014

c++: writing strings to file by removing first char -



c++: writing strings to file by removing first char -

i reading input file line line using ifstream , getline (say in string line). , have output string line file removing first character of it. doing eraseing first character of line , outputting using ofstream. there improve method (means relatively faster one) ? have millions of strings. (note not true lines, first line of every 10 line).

you output actual string pointer plus one:

outputstream << (line.c_str() + 1);

however, should improve check string not empty first, or might end accessing illegal pointer.

if want output substring there's std::string::substr function. or utilize std::ostream::write function combined pointer arithmetic outlined above:

outputstream.write(line.c_str() + 1, 9); /* 1st 10th character */

for above, have create sure length of string @ to the lowest degree 10 characters.

note: not utilize "hacks" ones outlined in answer, unless in extreme situations. substring function there reason, , 1 recommend using.

c++

No comments:

Post a Comment