I am reading an input file line by line by using ifstream and getline (say in string line). And I have to output string line to a file by removing first character of it. What I am doing is eraseing first character of line and outputting it using ofstream. Is there any better method to do it (means relatively faster one) ? I have millions of strings. (note that this is not true for all the lines, only for first line of every 10 line).
I am reading an input file line by line by using ifstream and getline
Share
You could output the actual string pointer plus one:
However, you should better check that the string is not empty first, or you might end up accessing an illegal pointer.
If you want to output a substring there’s the
std::string::substrfunction. Or use thestd::ostream::writefunction combined with the pointer arithmetic outlined above:For the above, you have to make sure the length of the string is at least ten characters.
Note: I personally would not use “hacks” as the ones outlined in this answer, unless in extreme situations. The substring function is there for a reason, and is the one I would recommend using.