Solved

Writing to a text file

Posted on 2011-02-21
1
856 Views
Last Modified: 2012-05-11
Hi,

I need to write to a text file in a loop. Every time I go into the loop I will loop through a text file line by line and every line I finds in this text file I need to write that line into another text file and following that line I need to add another line from some other processing result. Then the program will resume reading another line from that first text file and add it to the second file again and so forth until we reached the end of first file.

Can somebody tell me how can achieve this in c# please.
0
Comment
Question by:ube100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 34947457
It should something like this:

using System;
using System.IO;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader inFile = null;
            StreamWriter outFile = null;

            try
            {
                inFile = new StreamReader(@"C:\Temp\YourInputFile.txt");
                outFile = new StreamWriter(@"C:\Temp\YourOutputFile.txt");

                for (var line = inFile.ReadLine(); line != null; line = inFile.ReadLine())
                {
                    outFile.WriteLine(line);

                    // Your Process
                    outFile.WriteLine(" your process line goes here ");
                }
            }
            finally
            {
                if (inFile != null) inFile.Close();
                if (outFile != null) outFile.Close();
            }
        }
    }
}

Open in new window

0

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question