Firstly, I want to insert characters into a text file. I want to print the characters (DAD1) into the middle of the text file (HERE with DA D1) and the text file looks like this =>
@2100
AB CD EF 12 43 56
A3 B2 34 56 .....
...............78
@ffb4
FF FF FF FF(***HERE***)
@ffc0
FA 21 FA 21 ....
I tried to use fprintf but it prints at the end of the file. Here is my coding part =>
fp = fopen("testing.txt", "r+");
if (fp)
{
while (c != EOF)
{
c = getc(fp);
if (c == '@')
{
cout << c ;
while (c != '\n')
{
c = getc(fp);
cout << c ;
}
addr ++ ;
}
else
{
if (addr == 1)
{ ***DO SOMETHING***}
else if (addr == 2)
{
char higha = hia.to_ullong() + ((hia.to_ullong() >= 10) ? 55 : 48);
char highb = hib.to_ullong() + ((hia.to_ullong() >= 10) ? 55 : 48);
char lowa = loa.to_ullong() + ((loa.to_ullong() >= 10) ? 55 : 48);
char lowb = lob.to_ullong() + ((lob.to_ullong() >= 10) ? 55 : 48);
fprintf(fp, "%c", higha);
fprintf(fp, "%c", highb);
fprintf(fp, " ");
fprintf(fp, "%c", lowa);
fprintf(fp, "%c", lowb);
}
}
}
}
I try to use SEEK_CUR but I don't understand how it works. Or have another way to do it???
Unless you're working on a file larger than 1/2 of your RAM, you will need to load the text file into RAM, detect it's length there, modify it, then write it back to disk. If the file is large then you would need to make a file stream driver, but unless you're trying to prevent SSD trashing then your best bet is the Read-Modify-Write method.