none
replacing string in ap from a saved file RRS feed

  • Question

  • I use the following to read out numbers from a text file and replace local int values...

    string save1 = file.ReadLine();
    int.TryParse(save1, out number1);

    how can i use the same method to extract a string from text file and replace a local string value? (eg, i have string message = "..."; and when i import from the text file, it turns into string message = "hello";)

    Much thanks in advance!


    • Edited by J3KB0T Monday, January 15, 2018 2:08 AM
    Sunday, January 14, 2018 10:14 PM

Answers

  • Hello J3KBOT,

    It is different job between declaration and assignment of variable. Something is like below.

    string name; //declaration of name variable
    name = "unregistered";//assignment of name variable

    And the combination statement is as below.

    string name = "unregistered";

    But the same variable must be declared only once. the assignment of variable could be executed multi times. If you want to assign again, you can't redeclare the variable again. For your situation you just edited as below.

       name = file.ReadLine();

    Best regards,

    Neil Hu


    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.

    • Edited by Fei HuModerator Tuesday, January 16, 2018 9:09 AM
    • Marked as answer by J3KB0T Tuesday, January 16, 2018 1:08 PM
    Tuesday, January 16, 2018 9:07 AM
    Moderator

All replies

  • Hello J3kBOT,

    If you want to change the certain line value, you need to read all text in memory. Than save the file at once. A simple example shows as below.

    	static void Main(string[] args)
    		{
    			string path = "../../TextFile1.txt";
    			string[] lines=File.ReadAllLines(path);
    			for (int i=0;i<lines.Length;i++) {
    				if (i == 1) lines[i] = "10000";
    			}
    
    			File.WriteAllLines(path,lines);
    		}

    Best regards,

    Neil Hu


    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 15, 2018 2:05 AM
    Moderator
  • sorry, i edited my question to clarify better. All i need is a command to read out one line, and replace a local string value.
    Monday, January 15, 2018 2:13 AM
  • Hello J3KBOT,

    What do you mean the "same method" in your statement, does it mean file.ReadLine method? If so, the Readline method will return string type and you don't need to do extra job. something is like below.

    string message = file.ReadLine();

    Or

    string save1 = file.ReadLine();
    
    string message = save1;

    If not, please tell me the detailed information about the "same method" meaning.

    Best regards,

    Neil Hu


    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 15, 2018 2:57 AM
    Moderator
  • Hello,

    Perhaps you can show us what you have tried already.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 15, 2018 3:52 AM
    Moderator
  • that seem to be the logical way of going about it, however i get an error "you cannot declare in this scope because it is being used in en enclosing local scope to define a local or parameter". at the start of my app i declared 

    string name = "..."; (to indicate name has not been registered)

    when registered, the string "name" will now show the persons name. 

    to save this progress, i use 

    using (System.IO.StreamWriter file =
                        new System.IO.StreamWriter(settings32))
                    {
                        file.WriteLine(name.ToString());

                     }

    to load this process, i tried to use

    if (System.IO.File.Exists(settings32))
                    {
                        using (System.IO.StreamReader file =
                            new System.IO.StreamReader(settings32))
                        {

                            string name = file.ReadLine();

                         }

    but i get the "scope" error. this works fine when i do it for my ints that i declared along with my string. but for some reason the string gives me errors.

    string registration = @"registration.txt"; int age = 0; string name = "unregistered"; flag1: Console.WriteLine("please enter command"); string command = Console.ReadLine(); if (command.Contains("whois")) { Console.WriteLine($"Name: {name}, Age: {age}"); goto flag1; } else if (command.Contains("regisetr")) { Console.WriteLine("enter name"); name = Console.ReadLine(); Console.WriteLine("enter age"); age = Console.ReadLine(); goto flag1; } else if (command.Contains("save")) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(registration)) { file.WriteLine(name.ToString()); file.WriteLine(age.ToString()); } goto flag1; } else if (command.Contains("load")) { if (System.IO.File.Exists(registration)) { using (System.IO.StreamReader file = new System.IO.StreamReader(registration)) { //I GET SCOPE ERROR BELOW FOR NAME

    string name = file.ReadLine(); string save1 = file.ReadLine(); int.TryParse(save1, out age); } } goto flag1; }




    • Edited by J3KB0T Monday, January 15, 2018 10:23 PM
    Monday, January 15, 2018 1:49 PM
  • Hello J3KBOT,

    It is different job between declaration and assignment of variable. Something is like below.

    string name; //declaration of name variable
    name = "unregistered";//assignment of name variable

    And the combination statement is as below.

    string name = "unregistered";

    But the same variable must be declared only once. the assignment of variable could be executed multi times. If you want to assign again, you can't redeclare the variable again. For your situation you just edited as below.

       name = file.ReadLine();

    Best regards,

    Neil Hu


    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.

    • Edited by Fei HuModerator Tuesday, January 16, 2018 9:09 AM
    • Marked as answer by J3KB0T Tuesday, January 16, 2018 1:08 PM
    Tuesday, January 16, 2018 9:07 AM
    Moderator
  • very good, thank you very much
    Tuesday, January 16, 2018 1:09 PM