I'm using both of these functions (seekp and seekg) for my ifstream and ofstream objects, but no matter what I try I'm still printing to the first line.
What my program does is open an html file, read in a line, convert all tags to uppercase or lowercase, then output the line back into the file. All this works fine for the first line.. but my seekp, seekg functions aren't working right, and it keeps reading and outputting to the same line. How can I make it in sync with my currentLine variable? Consider the following code:
Code:
void CHTMLConvertDlg::OnConvert()
{
UpdateData(TRUE);
ifstream fin;
ofstream fout;
long int currentLine = 0;
fin.open(m_sFileName, ios::nocreate);
if (fin.fail())
{
AfxMessageBox("Failed to open file!");
} else {
do
{
char line[255];
if (fin.is_open())
{
fin.seekg(currentLine);
fin.getline(line, 255, '\n');
currentLine++;
} else {
fin.open(m_sFileName, ios::nocreate);
fin.seekg(currentLine);
fin.getline(line, 255, '\n');
currentLine++;
}
CString entireLine = line;
CString subLine;
signed int tempOffset[2] = {0,0};
signed int tagOffset[2] = {0,0};
while (1)
{
subLine.Empty();
tempOffset[0] = entireLine.Find('<',tagOffset[0]);
tempOffset[1] = entireLine.Find('>',tagOffset[1]);
tagOffset[0] = tempOffset[0];
tagOffset[1] = tempOffset[1];
if (tagOffset[0] == -1)
{
break;
}
for (int i = 1; i < ((tagOffset[1]) - (tagOffset[0])); i++)
{
subLine = subLine + entireLine.GetAt(tagOffset[0] + i);
}
//AfxMessageBox(subLine);
if (m_bCase)
{
subLine.MakeUpper();
} else {
subLine.MakeLower();
}
for (i = 0; i < subLine.GetLength(); i++)
{
entireLine.SetAt((tagOffset[0] + 1 + i), subLine.GetAt(i));
}
tagOffset[0] = (tagOffset[1] - 1);
tagOffset[1]++;
}
fin.close();
fout.open(m_sFileName, ios::ate);
fout.seekp(currentLine);
fout.write(entireLine, entireLine.GetLength());
fout.close();
} while (!fin.eof());
}
fin.close();
}