开发者

Editing a line in a text file without using pointers?

I am trying to edit a line of a text file (.Hex file) containing all Hex characters without using pointers and in a more efficient way.

It takes so long because the program I have to edit some (around 30x4 bytes or 30 float values from the address values of hex file).

Every time the program replaces one byte, it searches the complete file and replaces the values, and copy back back again the new file to another file. This process repeats 30 times, which is quite time consuming and hence not looks appropriate.

What would be the most efficient method?

public static string putbyteinhexfile(int address, char data, string total)
{

    int temph, temphl, tempht;
    ushort checksum = 0;
    string output = null, hexa = null;
    StreamReader hex;
    RegistryKey reg = Registry.CurrentUser;
    reg = reg.OpenSubKey("Software\\Calibratortest");
    hex = new StreamReader(((string)reg.GetValue("Select Input Hex File")));
    StreamReader map = new StreamReader((string)reg.GetValue("Select Linker Map File"));
    while ((output = hex.ReadLine()) != null)
    {
        checksum = 0;
        temph = Convert.ToInt16(("0x" + output.Substring(3, 4)), 16);
        temphl = Convert.ToInt16(("0x" + output.Substring(1, 2)), 16);
        tempht = Convert.ToInt16(("0x" + output.Substring(7, 2)), 16);
        if (address >= temph && 
            address < temph + temphl && 
            tempht == 0)
        {
 开发者_如何学JAVA           output = output.Remove((address - temph) * 2 + 9, 2);
            output = output.Insert((address - temph) * 2 + 9, 
                     String.Format("{0:X2}", Convert.ToInt16(data)));

            for (int i = 1; i < (output.Length - 1) / 2; i++)
                checksum += (ushort)Convert.ToUInt16(output.Substring((i * 2) - 1, 2), 16);

            hexa = ((~checksum + 1).ToString("x8")).ToUpper();
            output = output.Remove(temphl * 2 + 9, 2);
            output = output.Insert(temphl * 2 + 9, 
                                   hexa.Substring(hexa.Length - 2, 2));
            break;
        }
        else total = total + output + '\r' + '\n';
    }

    hex.Close();
    map.Close();

    return total;
}


Assuming you don't want to massively rewrite your existing logic which does 'for each line, do this search and replace logic', I'd think the simplest change would be:

var lines = File.ReadAllLines(filePath);
foreach (change to make)
{
    for (int i = 0; i < lines.Length; i++)
    {
        // read values from line
        if (need_to_modify)
        {
            // whatever change logic you want here.
            lines[i] = lines[i].Replace(...);
        }
    }
}
File.WriteAllLines(filePath, lines);

Basically, you'll still do the logic you have now, except:

  1. You read the file once instead of N times
  2. you get rid of streamreader / streamwriter work
  3. you do your changes on the array of strings in memory


string fileName = "blabla.hex";
StreamReader f1 = File.OpenText(fileName);
StreamWriter f2 = File.CreateText(fileName + ".temp_");

while (!f1.EndOfStream)
{
    String s = f1.ReadLine();
    //change the content of the variable 's' as you wish 
    f2.WriteLine(s);   
}

f1.Close();
f2.Close();
File.Replace(fileName + ".temp_", fileName, null);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜