Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3739
  • Last Modified:

Using C# open text file and add text to the beginning and end of each line

Hi,

I want to open a text file then add an open quotation mark to the beginning of each line and a quotation mark and a plus sign to the end of each line.

I am trying to do this in C# but cannot find any good examples.

Thanks for any help,
Bonnie
0
Bonnie_K
Asked:
Bonnie_K
1 Solution
 
Sh4d1Commented:
I suggest to read file in for/while read all lines into array and then with regular expressions add at the start and end of line what you want, then write changes back to file.
0
 
raterusCommented:
There are plently of ways to do this, here is one of them:

        String inputFile = Server.MapPath("./Test.txt");
        StreamReader sr = new StreamReader(inputFile);
        String filecontents = sr.ReadToEnd();
        sr.Close();

        StringReader gr = new StringReader(filecontents);
        StreamWriter gw = new StreamWriter(inputFile, false);

        while (gr.Peek() != -1)
        {
            gw.WriteLine("\"" + gr.ReadLine() + "\"+");
        }

        gr.Close();
        gw.Close();
0
 
Bonnie_KAuthor Commented:
Thank you very much, works perfectly!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now