-2

i am a newbie in here so i apogolize if i make mistakes. Now i have a program that takes a random int from a text file that contains one million digits of number Pi. the lenght is also random but it shouldn't be longer than 128 digits. I looked for it so much but i was able to find this code that makes my computer get frozen by the cpu usage being %100.

string[] allLines = File.ReadAllLines(Application.StartupPath+ "\\pi.txt");
        Random rnd1 = new Random();
        textBox2.Text=(allLines[rnd1.Next(allLines.Length)]);

also when i got the random ints, i need it to be printed into textBox2's text. i am terribly sorry for being such a noob.

PrimeGamer25
  • 33
  • 1
  • 7
  • I'm sorry, but I don't understand what your issue is. What is not working? – rory.ap Dec 06 '18 at 13:35
  • the pc is getting stuck and not printing out the random value i want – PrimeGamer25 Dec 06 '18 at 13:36
  • It's not getting stuck, at least not in the code you provided. How big is the file? – TypeIA Dec 06 '18 at 13:37
  • what *is* it printing out? What do you mean "stuck"? You're being too vague. What *exactly* is happening? Like literally, what is happening? – rory.ap Dec 06 '18 at 13:37
  • 976kb and it contains a million digits – PrimeGamer25 Dec 06 '18 at 13:37
  • it becomes unusable and until i shut down the app it stays like that – PrimeGamer25 Dec 06 '18 at 13:38
  • 1
    Well have you tried debugging it? Stepping through the code and watching what it's actually doing, observing the values in your variables? – rory.ap Dec 06 '18 at 13:40
  • A little more code and the first few lines of ```pi.txt``` would help to understand the problem. How big is pi.txt anyways(3 MB? one for the digit, one for \r one for \n times 1 Million)? Might be simply loading time of the file which is the problem. You could debug it line by line to identify the line which is causing the issue. – FrankM Dec 06 '18 at 13:42
  • umm that is the problem, whenever i execute the program to understand whats happening the pc get unusable i can't click anything, its got 4gigs of ram tho:) – PrimeGamer25 Dec 06 '18 at 13:43
  • I'm not talking about just executing it. I'm talking about debugging it, setting a break point, etc. – rory.ap Dec 06 '18 at 13:45
  • well i found out when i put the breakpoint to the start button in my forms app. thanks to you rory.ap, and i Confirmed that the problem is same that FrankM says thanks to him and all of you. – PrimeGamer25 Dec 06 '18 at 13:48

1 Answers1

0

you are trying to erad all the file just for couple of bytes. consider the solution below.

Please Note that I didnt include Random method.

byte[] byteArray = new byte[10];
var file = 'path/to/your/file';
using (BinaryReader reader = new BinaryReader(new FileStream(file, FileMode.Open)))
{
    reader.BaseStream.Seek(<startingPosition>, SeekOrigin.Begin);
    reader.Read(byteArray, 0, <numberOfLinesToRead>);
}

string result = System.Text.Encoding.UTF8.GetString(byteArray);
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72