Solved

Writing to a text file

Posted on 2011-02-21
1
854 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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