And any way to improve this?
whats wrong with this code, I'm trying to parse a .js file and replace all the ";" with ";\n" i.e add a new line after each ";". So I load the file into a char[] buffer then assign a string to this contents of this buffer. Then loop char by char through using an iterator and check for a ";", if found use replace. So int i gets to about 85898 then crashes with unknown error, 'i' should reach about 175653. It does work up till it crashes. And, is this not a simpiler way to load a file into a buffer, there is in C.
Code:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <stdlib.h>
int main()
{
int length;
char * buffer;
std::string str;
std::ifstream myfile;
std::ofstream myfile2;
myfile.open ("c:\\c.js",std::ios::binary);
myfile2.open ("c:\\c2.js", std::ios::out);
myfile.seekg(0,std::ios::end);
length = myfile.tellg();
myfile.seekg(0,std::ios::beg);
buffer = new char [length];
myfile.read (buffer,length);
str = buffer;
delete[] buffer;
std::string::iterator it;
std::string str2;
int i = 0;
for ( it=str.begin(); it < str.end(); it++ )
{
str2 = *it;
//std::cout << i << std::endl;
if(str2 == ";")
{
str.replace(i, 1, ";\n");
}
i++;
}
myfile2 << str;
myfile.close();
myfile2.close();
return 0;
}