Solved

Writing to a text file

Posted on 2011-02-21
1
850 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
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now