0

I'm programming an old-school mmorpg in win32 (It's supposed to run on very old machines), and I want to read a file that can be either ASCII or Unicode (rarely), and count the number of lines in it. due to the fact that those machines have very very low memory, I can't keep more than 4KB at once in the RAM.

any ideas? thanks

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
user1114365
  • 35
  • 2
  • 3
  • 2
    You only need to have enough storage for *one line* in the file. Not even that if you only have to count lines. Simply use the fgets() function. Keep calling it until it returns NULL. Check the returned string for `\n` at the end, increment the line counter when you see it. – Hans Passant Apr 05 '12 at 18:12
  • Is this the client, or the server? Surely you can't make a game "massively multiplayer" without comparatively massive resources. – Wooble Apr 05 '12 at 18:15

2 Answers2

2

Read the file in small chunks at a time, say 1/10th the size of your memory budget. Count the line ends in each chunk.

The only real problem you face is that you need to deal with cr+lf pairs that span from the end of one chunk to the beginning of the next.

As an aside, surely you mean 4MB rather than 4KB?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Just to propose something a little different...

type foo.txt|find /C /V "~`!@#$%^&*()_+"

Or really just the following I think... been awhile

find /C /V "" foo.txt
ficuscr
  • 6,975
  • 2
  • 32
  • 52