Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing to a text file

Posted on 2011-02-21
1
Medium Priority
?
857 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 1000 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] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

705 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