InsertLine()

From Hack Wars Wiki

Jump to: navigation, search

This function inserts the specified text after the given line in the file.

void insertLine(string filename,string content, int linebefore)
{
	if(!fileExists(file)) // if the file does not exist, create a new file containing content
	{
		writeLine(file,content);
	}
	else if(linebefore>=countLines(file)) // if linebefore is not in the file, add it to the end of the file
	{
	writeLine(file,content);
	}
	else
	{
		string newcontent = "";
		int totalLines = countLines(filename);
		for(int j = 0; j < totalLines; j++)
		{
			if(j==linebefore)
			{
				newcontent += readLine(filename,j);
				newcontent += char(10);
				newcontent += content;
				newcontent += char(10);
			}
			else
			{
				newcontent += readLine(filename,j);
				newcontent += char(10);
			}
		}
		clearFile(filename);
		writeFile(filename,newcontent);
	}
}
TTJ 18:35, 28 November 2008 (UTC)
Personal tools