1

I am creating a small pet cryptography project and am reading text from a text file, modifying it while each line is in an array, and then submitting it back to its text file. The issue is adding the string back to the file, as the text file's current text is simply deleted as of how the program is now. More specifically I want to bring all of the strings in the array into a single string, and this is where I believe the fault is. Here is the code I have written.

static void Main(string[] args)
    {
        string[] getAllText = File.ReadAllLines("H:\\BetaText.txt");

        File.WriteAllText("H:\\BetaText.txt", string.Concat(Lock(getAllText)));
        System.Diagnostics.Process.Start("H:\\BetaText.txt");
    }

Lock is simply a method in my program that returns an encrypted array of strings. The text file is confirmed to be correctly parsed in and the modification return does indeed return the array as I wanted. The issue continues to be my string.Concat() statement. I do know how to fix this issue with a multi-line statement but I would like to avoid this and learn why the Concat() statement is not working the way I believed it would. I have also used the string.Join method with a "" delimiter. Looking through StackOverflow I have not seen this answered and according to MSDN documentation I do not believe I should be having this issue. Thank you.

17
  • Wouldn't the code you posted only put a single item into the string[]? It seems like you would need to perform a Split() method on some delimiter within the the string result of File.ReadAllLines in order to get multiple items within the string[]. Concatenating a single string[] item would only return a single string. Commented Mar 3, 2016 at 16:29
  • 1
    Is there a particular reason you are encrypting each string in the array, when you could read the entire file into a byte[], encrypt, and then write back the encrypted bytes? Commented Mar 3, 2016 at 16:29
  • 1
    So what does string.Concat return and what did you expect it to return? Commented Mar 3, 2016 at 16:38
  • @Russ: Each line of my text file is added into an individual string in my array when I pull it with ReadAllLines Commented Mar 3, 2016 at 16:39
  • @series0ne My intention is to learn how to use this method to use the encryption before moving onto better ways to do it. When I reach that step I will be on Code Review. Better to learn how to do it the easy way now and learn how to do it the best way once I have a good grasp of it. Also I am not yet comfortable modifying bytes as I have yet to work with individual bytes. Commented Mar 3, 2016 at 16:42

3 Answers 3

2

You can use String.Join to join string array into a single string.

String.Join ("", getAllText);

Also you can use Aggregate

getAllText.Aggregate((c, n) => string.Format("{0}{1}", c, n);

Using aggregate you can construct more sophisticated examples. For example this is how you can separate them with comma

getAllText.Aggregate((c, n) => string.Format("{0}, {1}", c, n));
Sign up to request clarification or add additional context in comments.

4 Comments

I have not seen this method before and am not aware how to use it. Can you give an example of what f and n are? I have checked MSDN but I do not fully understand.
I changed f to n. I understand it as c-current set and n - next element. Try this link stackoverflow.com/questions/7105505/…
I see absolutely no reason why one should use Aggregate like that when we have string.Join and string.Concat.
One problem with using Aggregate instead of string.Concat or string.Join is that a new temporary string instance is created for each iteration. That is wasteful. The methods string.Concat and string.Join can be optimized to use a StringBuilder or something even better.
1

Or simply joined with a ','. Choose another delimiter if you wish

 var writeText = string.Join(", ", Lock(getAllText).Select(v => v.ToString()));
 File.WriteAllText("H:\\BetaText.txt", writeText);

2 Comments

Also attempted as in the description. The output was the same. An empty text file.
Try the updated answer and check to see if writeText is empty?
1

So what you are doing is reading a file, line-by-line into a string[] and then encrypting each line, and then committing those lines back into a file.

Personally, I think your approach is wrong (gut feeling, but here's why)

File.ReadAllLines creates an array by splitting the file using line breaks (Environment.NewLine, "\r\n", "\n"...) - what happens if your encryption algorithm happens to encrypt some particular byte as a newline character? - all of a sudden your line breaks are different - therefore doing the reverse would essentially fail (I think, since decrypting the file "might" be working with different line breaks)

A better solution would be this:

byte[] unencryptedBytes = Encoding.UTF8.GetBytes(File.ReadAllText(filename));
byte[] encryptedBytes = Lock(unencryptedBytes);
File.WriteAllText(filename, Encoding.UTF8.GetString(encryptedBytes));

I know this is not technically what you are asking for (sorry) - I just have this gut feeling that what you're doing, might not work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.