Solved

Writing to a text file

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data organization issue 7 56
VB.net/VSTO Excel Add-in 2 38
PowerShell:  Use of subproperties in a Select statement 7 69
Need to find a web event on the IIS server 4 54
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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

734 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