DjangoBlockchain
DjangoBlockchain

Reputation: 564

Truncating a txt file by 'x' bytes in C#

I want to get rid of the last line of a text file after reading the contents. The text files are quite large, so Reading/Writing is not an option due to performance issues.

My current idea is to calculate the number of bytes the last line represents (along with the carry return) and truncate the file.

A lot of the options I saw referred to using "Filestream.setLength()", and I am confused as to how this works.

Wouldn't this just write the file back, but stop the file at a certain number of bytes, since the 'read' function reads in the bytes, and writes them back to a buffer? Or would I be able to use this function as I am reading, and move the "end position" of the text file back, say 24 bytes?

This is the current code I am using

try
            { 
            //reading
            using (StreamReader reader = new StreamReader(filePath))
            {
                    while (!reader.EndOfStream)
                    {
                        //gets the line
                        line = reader.ReadLine();
                        if (!line.StartsWith("KeyWord", StringComparison.InvariantCultureIgnoreCase))
                        {
                            //add number of lines
                            lineCount += 1;
                        }
                        else
                        {
                            //Finds the occurence of the first numbers in a string
                            string resultString = Regex.Match(line, @"\d+").Value;
                            long lastLineBytes = 0;
                            foreach (char c in line)
                            {
                                //each char takes up 1 byte
                                lastLineBytes++;         
                            }
                            //carriage return
                            lastLineBytes += 2;
                            long fileLength = new FileInfo(filePath).Length;
                            Trace.WriteLine("The length of the file is " + fileLength);

                            //the size of the file - the last line
                            //truncate at this byte position, and we will be done. 
                            long newFileLength = fileLength - lastLineBytes;
                            //Truncation goes ehre
                        }
                }
          }
        }
        catch (Exception e)
        {
            throw e;
        }

Upvotes: 1

Views: 807

Answers (1)

Gusman
Gusman

Reputation: 15151

If you change the size of the stream it will rewrite just the file table indexes to point just to the left bytes.

For your second question, yes, you can use this to read the content (in this example I assumed ASCII encoding, use the appropiate encoder).

FileStream str = //get the stream
byte[] data = new byte[str.Length];
str.Read(data, 0, data.Length);
string theContent = System.Text.Encoding.ASCII.GetString(data);

Upvotes: 1

Related Questions