2

I am writing lines to text file. Is there a way to limit the maximum number of lines in a text file. So that I am not allowed to write after that limit.

Or

if i continue to write after the max line limit the oldest written lines are deleted to accommodate the newly added lines.

user2968369
  • 265
  • 1
  • 4
  • 10

3 Answers3

3

There is ... but you shouldn't be hitting it ... And if you ARE ... well, maybe a text file isn't what you're looking for.

Size wise, a file has different limitations depending on your file system ... NTFS (almost 16TB), FAT (fat 32 is almost 4GB), unix file systems will have their limitations, and so on ...

Like they suggest, your limit will be the size of the file.


As for your comment:
You can set the limit to whatever you wish. What you do then is up to you ... if you decide to overwrite the file, it'll delete and start afresh. if you decide to append, it'll append to the end.

I would suggest create a queue of a 100 strings, and if you push new ones, drop the last one in the queue. Then you can just have that class save the log whenever, wherever and however you want.

Community
  • 1
  • 1
Noctis
  • 11,507
  • 3
  • 43
  • 82
  • I just want to set my own limit. That e.g. if I set 100 line limit then text file size can’t exceed that many lines – user2968369 Nov 15 '13 at 12:30
  • thanks for the answer. Actually i am logging data in a file and the the app will be running for years so I can run into hitting the max file limit thats why i thought of limiting the no of lines, but i understand from your answer that it is a wrong approach. file size check is the right way – user2968369 Nov 15 '13 at 12:42
  • like Nanhydrin said, probably be easier with some framework. And worst case, once you hit a file size, you can just start with a new file, and maybe zip the old one in case you need it in the future ... – Noctis Nov 15 '13 at 12:44
  • 1
    Cheers, hope it helped. OnoSendai answer is a worthy answer :) – Noctis Nov 15 '13 at 14:03
1

Create your own method like this

public void writeLines(string filePath,string[] lines,int limit)
{
    var buffer=Enumerable.Empty<string>();
    if(File.Exists(filePath))
        buffer=File.ReadAllLines(path);
    File.WriteAllLines(filePath,lines);
    int range=limit-lines.Length+buffer.Count;
    File.AppendAllLines(filePath,buffer.Take(range));
}
Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

The answer to the first question is very simple:

  • Know your storage limit.
  • Know your current file size.
  • If the new line's length plus current file size is more than the storage limit, don't append it.

Now, the second is kind of tricky. As pointed out by several participants on this thread, line-by-line threshold manipulation can be very very costly.

Let's do some napkin simulations, and assume you're inserting 1024 bytes (1KB) at each Append, and your storage limit is 1GB. Once you insert the last line (n. 1048576), you decide you need to remove the first line. There's a few ways to accomplish this, but the majority of them will involve loading the whole collection minus the initial line elsewhere (memory, disk, you name it) and Appending the new one. Not exactly the most practical approach - you'll be manipulating a stack a million times larger than the content you want to add, just for the sake of adding it.

Solution 1

  • Cursor buffer
    • On our example you have 1048576 possible entries (1KB records on 1GB file).
    • Start filling it up; save the current position (cursor) elsewhere.
    • Once you reach the limit, your cursor resets; you overwrite position 0, then 1, and so forth.
    • Advantages: Very low disk cost.
    • Disadvantages: You'll need to keep track of your current cursor somewhere.

Solution 2

  • Text blocks
    • Assume 1GB storage, and max 1MB files for this example.
    • Start filling up File #0.
    • Once it reaches 1MB, close it. Open File #1. Rinse and repeat.
    • Once you fill up file #1023 (thus reaching the 1GB max), delete oldest file (#0). Create file #1024. Continue your logging.
    • Advantages: Low manipulation cost - you only run one delete operation.
    • Disadvantages: You don't delete only one entry - you delete a whole block.
OnoSendai
  • 3,960
  • 2
  • 22
  • 46