none
Modifying the value within text file automatically RRS feed

  • Question

  • I have text file includes words and equivalent value as in format like in below

        A = Hello
        B = My
        C = Brother
        .
        .
        z = we

    I used the following code in order to read and modified value manually 

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.IO;

        namespace ConsoleApplication17
        {
        class Program
        {
            const string FILENAME = @"c:\temp\test.txt";
            static void Main(string[] args)
            {
                StreamReader reader = new StreamReader(FILENAME);

                Dictionary<string, string> dict = new Dictionary<string, string>();

                string inputLine = "";
                while((inputLine = reader.ReadLine()) != null)
                {
                    string[] splitArray = inputLine.Split(new char[] { '=' });
                    dict.Add(splitArray[0].Trim(), splitArray[1].Trim());
                }
              }
           }

         }

    I need to make change be automatically instead from change it manually, In way as text file includes lines "as shown up" each lines have character and the equivalent word (such as A = Friend, B= Love), what I need is to shift equivalent words of characters at each 1 hr by 1, the result should be after one hour (A = Love, B= Friend) 
    Friday, January 19, 2018 11:15 AM

Answers

  • Then I think you can use it. Be aware the copy is that you cannot use the same collection because you cannot use an item which is already changed (You can of course as well copy the first item). Also I've not taken time to do a loop At the end. I changed that in the code below, now you can use it with every length of items.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Item> Items = new List<Item>();
                Items.Add(new Item() { First = "A = ", Second = "Hello" });
                Items.Add(new Item() { First = "B = ", Second = "My" });
                Items.Add(new Item() { First = "C = ", Second = "Brother" });
                var ItemsDup = Items.Select(x => x.Second).ToArray();
                for (int i = 0; i < Items.Count-1; i++)
                    Items[i].Second = ItemsDup[i+1];
                Items[Items.Count - 1].Second = ItemsDup[0];
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(Items[i].First.ToString() + Items[i].Second.ToString());
                }
                Console.ReadKey();
            }
        }
        class Item
        {
            public string First { get; set; }
            public string Second { get; set; }
        }
    }


    Success Cor



    • Edited by Cor Ligthert Tuesday, January 23, 2018 6:37 PM
    • Marked as answer by mkf-iq Saturday, January 27, 2018 4:38 PM
    Tuesday, January 23, 2018 6:36 PM

All replies

  • Maybe can you ask your school teacher to help you. These forums are not to help with school assignments, you learn nothing from that. 

    Success Cor

    Friday, January 19, 2018 12:21 PM
  • Maybe can you ask your school teacher to help you. These forums are not to help with school assignments, you learn nothing from that. 

    Success Cor

    its not easy as you things its about sequence used with stego things.. I have text to be codes that saved in text file its like a puzzle I have put characters  from "a" to "z" and from "A" to "Z", each characters has equivalent word, but for security need not to be fixed I need it to be changing with time but automatically not manually. that what I need exactly 


    • Edited by mkf-iq Friday, January 19, 2018 5:19 PM
    Friday, January 19, 2018 5:17 PM
  • Hi mkf-iq,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about C# development. Since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, January 22, 2018 1:46 AM
  • Hi mkf-iq,

    For your question, I try to test the code. According to your code, you could get the corresponding value without change.

    But I am confused about your equivalent word. A=Friend, B=Love is not showing in the text file you provided for us. Please provide that how the value change you want automatically for us.

    If you want to do the change every hour, you could use Timer.

    https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 22, 2018 2:34 AM
    Moderator
  • Hi mkf-iq,

    For your question, I try to test the code. According to your code, you could get the corresponding value without change.

    But I am confused about your equivalent word. A=Friend, B=Love is not showing in the text file you provided for us. Please provide that how the value change you want automatically for us.

    If you want to do the change every hour, you could use Timer.

    https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks for replay Wendy, See as  an example 

         A = Hello

         B = My

         C = Brother 

    and let say shifting it so it will become 

         A = My

         B = Brother

         C =  Hello

    I need it to be shifted automatically  instead of changing it manually like after each hour it will be shifting  



    • Edited by mkf-iq Tuesday, January 23, 2018 2:19 PM
    Tuesday, January 23, 2018 2:18 PM
  • I don't think that if this is a school assignment your teacher will accept this as made by you.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Item> Items = new List<Item>();
                Items.Add(new Item() { First = "A = ", Second = "Hello" });
                Items.Add(new Item() { First = "B = ", Second = "My" });
                Items.Add(new Item() { First = "C = ", Second = "Brother" });
                var ItemsDub = Items.Select(x => x.Second).ToArray();
                Items[0].Second = ItemsDub[1];
                Items[1].Second = ItemsDub[2];
                Items[2].Second = ItemsDub[0];
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(Items[i].First.ToString() + Items[i].Second.ToString());
                }
                Console.ReadKey();
            }
        }
        class Item
        {
            public string First { get; set; }
            public string Second { get; set; }
        }
    }


    Success Cor

    Tuesday, January 23, 2018 5:32 PM
  • I don't think that if this is a school assignment your teacher will accept this as made by you.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Item> Items = new List<Item>();
                Items.Add(new Item() { First = "A = ", Second = "Hello" });
                Items.Add(new Item() { First = "B = ", Second = "My" });
                Items.Add(new Item() { First = "C = ", Second = "Brother" });
                var ItemsDub = Items.Select(x => x.Second).ToArray();
                Items[0].Second = ItemsDub[1];
                Items[1].Second = ItemsDub[2];
                Items[2].Second = ItemsDub[0];
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(Items[i].First.ToString() + Items[i].Second.ToString());
                }
                Console.ReadKey();
            }
        }
        class Item
        {
            public string First { get; set; }
            public string Second { get; set; }
        }
    }


    Success Cor

    Thank you Cor for interesting, it's not teacher things, see I created stego program (I can send you to see it), it text steganography I used characters from a to z and from A to Z and corresponding words, What I need with each run these words will be shifting with time and date, to explain when it at 6:00 pm 21-12-2018 A= hello, B= my, C = we, ......, so let it after 1 hr ( 7:00pm) in same day be shifted 3 times and so on so the sequance be same in both encrypter and decrypter .. Thanks for advance to your help

    Tuesday, January 23, 2018 6:18 PM
  • Then I think you can use it. Be aware the copy is that you cannot use the same collection because you cannot use an item which is already changed (You can of course as well copy the first item). Also I've not taken time to do a loop At the end. I changed that in the code below, now you can use it with every length of items.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Item> Items = new List<Item>();
                Items.Add(new Item() { First = "A = ", Second = "Hello" });
                Items.Add(new Item() { First = "B = ", Second = "My" });
                Items.Add(new Item() { First = "C = ", Second = "Brother" });
                var ItemsDup = Items.Select(x => x.Second).ToArray();
                for (int i = 0; i < Items.Count-1; i++)
                    Items[i].Second = ItemsDup[i+1];
                Items[Items.Count - 1].Second = ItemsDup[0];
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(Items[i].First.ToString() + Items[i].Second.ToString());
                }
                Console.ReadKey();
            }
        }
        class Item
        {
            public string First { get; set; }
            public string Second { get; set; }
        }
    }


    Success Cor



    • Edited by Cor Ligthert Tuesday, January 23, 2018 6:37 PM
    • Marked as answer by mkf-iq Saturday, January 27, 2018 4:38 PM
    Tuesday, January 23, 2018 6:36 PM
  • Then I think you can use it. Be aware the copy is that you cannot use the same collection because you cannot use an item which is already changed (You can of course as well copy the first item). Also I've not taken time to do a loop At the end. I changed that in the code below, now you can use it with every length of items.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Item> Items = new List<Item>();
                Items.Add(new Item() { First = "A = ", Second = "Hello" });
                Items.Add(new Item() { First = "B = ", Second = "My" });
                Items.Add(new Item() { First = "C = ", Second = "Brother" });
                var ItemsDup = Items.Select(x => x.Second).ToArray();
                for (int i = 0; i < Items.Count-1; i++)
                    Items[i].Second = ItemsDup[i+1];
                Items[Items.Count - 1].Second = ItemsDup[0];
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(Items[i].First.ToString() + Items[i].Second.ToString());
                }
                Console.ReadKey();
            }
        }
        class Item
        {
            public string First { get; set; }
            public string Second { get; set; }
        }
    }


    Success Cor



    ok, but not need to changed text file, it should be read text from file the rearnged them with time (not just counter) then saved it in another text file , can you helping me in that 
    Tuesday, January 23, 2018 6:44 PM
  • No, I'm not making your program, try that part first yourself and then if you wont succeed make a question for that. 

    Success Cor


    Tuesday, January 23, 2018 6:48 PM
  • No, I'm not making your program, try that part first yourself and then if you wont succeed make a question for that. 

    Success Cor


    ok no problem, thank at all

    Tuesday, January 23, 2018 7:22 PM
  • Then I think you can use it. Be aware the copy is that you cannot use the same collection because you cannot use an item which is already changed (You can of course as well copy the first item). Also I've not taken time to do a loop At the end. I changed that in the code below, now you can use it with every length of items.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Item> Items = new List<Item>();
                Items.Add(new Item() { First = "A = ", Second = "Hello" });
                Items.Add(new Item() { First = "B = ", Second = "My" });
                Items.Add(new Item() { First = "C = ", Second = "Brother" });
                var ItemsDup = Items.Select(x => x.Second).ToArray();
                for (int i = 0; i < Items.Count-1; i++)
                    Items[i].Second = ItemsDup[i+1];
                Items[Items.Count - 1].Second = ItemsDup[0];
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(Items[i].First.ToString() + Items[i].Second.ToString());
                }
                Console.ReadKey();
            }
        }
        class Item
        {
            public string First { get; set; }
            public string Second { get; set; }
        }
    }


    Success Cor



    ok, but not need to changed text file, it should be read text from file the rearnged them with time (not just counter) then saved it in another text file , can you helping me in that 
    Need just something from you, if these items A, B and C in text file how to shift it can you just helping me in that
    Saturday, January 27, 2018 6:14 AM
  • How to put the items in the array from a whatever datasource, is not a part of your question.

    Mark those replies which helped you with this part as answer and vote helpful post like that. 

    Than simply create a new question in which you ask how to do set your data in a generic list from a datasource (textfile or database for instance) showing how that datasource is organized. 


    Success Cor

    Saturday, January 27, 2018 12:41 PM
  • ok so can you please helping me how to using it with text file see I create a code as in below 

            static void Act()
        {
                int counter = 0;  
                string line;  

            // Read the file and display it line by line.  
            System.IO.StreamReader file =   
            new System.IO.StreamReader(@"c:\users\farttoos\documents\visual studio                             2017\Projects\ConsoleApp4\ConsoleApp4\words.txt");  
            while((line = file.ReadLine()) != null)  
            {  
               System.Console.WriteLine (line);  
           string[] split = line.Split('=');
           string First = split[0] + " = ";
           string Second = split[1]; 
           //actually you can use split[0] and split[1], the two above llines are for demo
           counter++;

            }  

    as you see split each one to "first" and "second" how to using your code to shifting the second one by any value let say 3 

    Saturday, January 27, 2018 4:44 PM