0

I have an original file that needs editing, I have managed to open this file and used code to correct the problems I have been asked to this has been done by changed the original file into a string, Now I need to save these changes, how do I save to a new file what is being displayed on the console? I have tried using stream writer but don't know how to save the edited string.

Monika
  • 2,172
  • 15
  • 24

2 Answers2

0

New answer based on new/detailed requirements: I modified your code and added some new lines.

string path = @"c:\temp\MyIOFile.txt";

            try
            {

                string file = File.ReadAllText(path);
                //The code wrote to the right hand side finds the file listed from my C drive

                string longstr = file;

                string[] strs = longstr.Split(':', '*');

                foreach (string ss in strs)
                {
                    Console.WriteLine(ss);
                }

                //before text is written, you say you want to modify it
                string newText = "*enter new file contents here*";

                //you can add new text (Append) or
                //change all the contents of the file
                //set the value of whatToDo to "Append" to add new text to the file
                //set the value of whatToDo to any value other than "Append" to replace
                //the entire contents of the filw with the data in variable newText
                string whatToDo = "Append";

                if (whatToDo == "Append")
                {
                    //append to existing text
                    //variable file contains old text
                    //varaible newText contains the new text to be appended
                    File.AppendAllText(path, newText);
                }
                else
                {
                    //creates new contents in the file.
                    //varaiable new text contains the new text representing
                    //file contents
                    File.WriteAllText(path, newText);
                }


                //string file = File.AppendAllText(@"C:\Users\path\.......");

            }
            catch (Exception ex)
            {
                Console.WriteLine("*** Error:" + ex.Message);
            }
            Console.WriteLine("*** Press Enter key to exit");
            Console.ReadLine();
        }

Original Answer

May be this could help:

    string path = @"c:\temp\MyIOFile.txt";
    if (!File.Exists(path))
    {
        // File does not exist - What do you want to do? 
    }
    try
    {
    // Open the file to read from and store result in a string variable
    string readText = File.ReadAllText(path);

    // modify the text somehow before appending to file
    string appendText =readText+ Environment.NewLine+ "This is extra text";
    File.AppendAllText(path, appendText, Encoding.UTF8);
    }
    catch (Exception ex)
    {
        Console.WriteLine ("***Error:" + ex.Message);
        // display errors
    }
NoChance
  • 5,632
  • 4
  • 31
  • 45
  • this didnt help, would it be of any help to insert my code that i have wrote – user2981019 Nov 11 '13 at 23:17
  • Yes. Also, tell us what you want exactly and what is your problem. – NoChance Nov 11 '13 at 23:18
  • okay so i have an original file with a block of text inside which i have to separate, do do this i have converted the file to a string so i can use a string.split withing my program, I have done this part now i want to save the edited text to a new file. – user2981019 Nov 11 '13 at 23:36
  • this is the code to convert the file to a string. string file = File.ReadAllText(@"C:\Users\path......"); – user2981019 Nov 11 '13 at 23:37
  • I assume you have to do some changes to the string you have read in the variable called 'file', so you do your changes and you save the resulting string to a file using the AppendAllText method as in the code above. I called the text coming from read operation 'readText' instead of 'file' - That is all. – NoChance Nov 11 '13 at 23:51
  • Which line is cause this error? Please copy the exact message. – NoChance Nov 12 '13 at 00:01
  • string fileedited = File.AppendAllText(@"C:\Users\path....."); this is the line causing the error and this is the error message "Error 1 No overload for method 'AppendAllText' takes 1 arguments C:\Users\admin\Documents\University work\Programming assignment\Chris Harrison Programming \Chris Harrison Programming t\Program.cs – user2981019 Nov 12 '13 at 09:19
  • Yes this is true, as you see in the code, the File.AppendAllText is coded with 3 arguments. The fist is the full path of the file (as you have), the second is the string to output to the file (or your variable containing it) and the third is the character encoding (you don't need to change this). So we have 3 args not 1. – NoChance Nov 12 '13 at 12:49
  • so how should I go about changing this, I had a thought of could I change the string back to a file and then use streamwriter to get around this problem – user2981019 Nov 12 '13 at 14:59
  • Please post your code that you currently have in the question text and I will fix it for you. – NoChance Nov 12 '13 at 15:01
0
string file = File.ReadAllText(@"C:\Users\path.......");
        //The code wrote to the right hand side finds the file listed from my C drive

 string longstr = file;

string[] strs = longstr.Split(':', '*');

foreach (string ss in strs)
        {
            Console.WriteLine(ss);
        }
string file = File.AppendAllText(@"C:\Users\path\.......");
        Console.ReadLine();