Click here to Skip to main content
15,889,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having the string value which is around 1750 characters. I am planing to save 500 bytes one by one since at a time at most 512 bytes can be saved . I am able to save statically considering the length limit. I want to do the same dynamically.

What I have tried:

Core code is as below:
try
           {
               string encLine = string.Empty;
               string encKey = string.Empty;
               using (StreamReader sr = new StreamReader(@"D:\test.txt"))
               {
                   // Read the stream to a string, and write the string to the console.
                   string line = sr.ReadToEnd();

                   int length = line.Length;
                   int nextlength = length - line.Substring(0, 500).Length;
                   for (int i = 0; i < 500; i++)
                   {
                       string test = line.Substring(0, 500);
                       SavePassword(test);
                       string test2 = line.Substring(500, nextlength);
                       SavePassword(test2);
                   }

                   //line.LastIndexOf(' ', 0, 100).ToString();
                   //Console.WriteLine(line);

               }
Posted
Updated 1-Dec-18 9:12am

As I said when you asked time last time: How to split a text file on reading the file in C#?[^]
Quote:
Use Streamreader.Read[^] and you can specify exactly how many characters to read at a time.

And as you were also told:
Quote:
Your text file is protected by GnuPG software. You have to decrypt it to be able to read its content.


Asking the same question does not change the answers, even if you want it too...
 
Share this answer
 
v2
So there's a couple of issues here that need to be addressed.

First off, the encoding type of the string is essential to know in advance if you're constrained to a specific byte length. We'll run under the assumption that it's UTF-8, but bear in mind that you may need to adjust for other encoding types.

Secondly, you have an arbitrary string length, so you cannot rely on taking specifically sized chunks of the string (otherwise 250 would be perfect). There's a few ways to skin this, but the easiest is to adapt your loop:

C#
using(StreamReader reader = new StreamReader(@"d:\test.txt"))
{
   var stringToChunk = reader.ReadToEnd();

   //Assuming UTF-8, so 1 byte per char
   var chunkLength = 512; //may as well max it out

   for(var i = 0; i < stringToChunk.Length; i += chunkLength)
   {
      //make sure we don't run past our buffer
      if(i + chunkLength > stringToChunk.Length)
      {
         chunkLength = stringToChunk.Length - i;
      }
      
      var chunk = stringToChunk.Substring(i, chunkLength);
      // do something with chunk
   }
}
 
Share this answer
 
string MainAddress = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789XXXXXX";
           int TotalLenght = MainAddress.Length;
           int Lenght_1 = 0, Lenght_2 = 0, Lenght_3 = 0;
           if (TotalLenght <= 35)
           {
               Lenght_1 = TotalLenght;
           }
           else
           {
               Lenght_1 = 35;
               if (TotalLenght <= 70)
               {
                   Lenght_2 = TotalLenght-Lenght_1;

               }
               else
               {
                   Lenght_2 = 35;
                   if (TotalLenght <= 105)
                   {
                       Lenght_3 = TotalLenght-(Lenght_1+Lenght_2);

                   }
                   else
                   {
                       Lenght_3 = 35;

                   }

               }
           }


           add1.Text = MainAddress.Substring(0, Lenght_1);
           add2.Text = MainAddress.Substring(Lenght_1, Lenght_2);
           add3.Text = MainAddress.Substring(Lenght_1 + Lenght_2,Lenght_3);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900