1

Hi all I'm having trouble writing a program that will record the frequency of how often a random number appears.

I'm using visual studio 2017 and c#

I have a file that contains the frequency

434
343
454
454

& then I've displayed the file in a list box like this

1    434
2    343
3    454
4    454

now I'm stuck on how to change data from the file so that the corresponding line increments by 1 when a number appears

Is their a function inside c# to allow one to go to a specific line in a text file then increase it's value by one or maybe go to a specific line in the listbox.

e.g. if 3 appears I want to go to line 3 and increase the number by 1.

I don't necessarily want anyone to write out the code for me, I would just love a pointer in the right direction

JasonCode
  • 11
  • 1
  • As long as I know you have to rewrite the whole file. Have a look at [https://stackoverflow.com/questions/1971008/edit-a-specific-line-of-a-text-file-in-c-sharp](https://stackoverflow.com/questions/1971008/edit-a-specific-line-of-a-text-file-in-c-sharp) or [Modify a specific line in a text file](https://stackoverflow.com/questions/30003630/modify-a-specific-line-in-a-text-file) – Mighty Badaboom Jul 17 '17 at 12:22
  • 2
    Create a keyvaluepair or a dictionary, store pairs like number-amount_of_appearances. When you find a number in a file, just increment it like structure[number]+=1; After parsing the whole file, generate a new one. – Sv Sv Jul 17 '17 at 12:22
  • only modify a text file when you are finished with your work in memory, i.e prefer to rewrite entire file only once – M.kazem Akhgary Jul 17 '17 at 12:28

1 Answers1

0

Instead of altering the original file, write out a new one with the frequencies. Then, when you are done, if you need it to replace the original, copy over it.

It's incredibly hard to edit a given file in place, especially if you need to change the position of other data.

Lou Franco
  • 83,503
  • 14
  • 127
  • 183