Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2007, 08:13 AM
Registered User
 
Join Date: Jun 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default reformatting text file

Hi,

I am pretty new to c# presently. I have a text file called tc.txt (comman separated) with the following content below:


T001,Dirks,c:\dirks\001.tif
T001,Dirks,c:\dirks\002.tif
T002,Matt,c:\matt\003.tif
T002,Matt,c:\matt\004.tif
T003,Ted,c:\Ted\005.tif
T003,Ted.c:\Ted\006.tif


When the file is read and written to another text file called output.txt (comma seperated). every line within the same folder should be written as follow;



T001,Dirks,c:\dirks\001.tif,c:\dirks\002.tif
T002,Matt,c:\matt\003.tif,c:\matt\004.tif
T003,Ted,c:\Ted\005.tif,c:\Ted\006.tif



Any help will be appreciated.



 
Old June 11th, 2007, 05:02 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

I am sending you a code snippet.....i am also new in c#,may be my code is little bit lengthy,but this code can do your job, if your text file contains many more data but in the same format as you have mentioned then also it will work fine.

The code is given below...



 static void Main(string[] args)
        {



            //FileStream fs = new FileStream("c:\\tc.txt", FileMode.Open, FileAccess.Read);
            StreamReader sr = File.OpenText("c:\\tc.txt");
            //fs.ReadByte();
            FileStream fs = new FileStream("c:\\output.txt",FileMode.Create,FileAc cess.Write);//this is the output file
            StreamWriter sw = new StreamWriter(fs);
            string str=null;
            int counter = 0;
            do
            {
                counter++;
                str = sr.ReadLine();
                if (str != null)//this loop prevents the last iteration
                {
                    int count = 0;
                    int position = 0;
                    if (counter % 2 == 0)
                    {
                        for (int i = 0; i < str.Length; i++)//this loop is for getting the position of the 2nd comma ","
                        {
                            if (str[i] == ',')
                            {
                                count++;
                                if (count == 2)
                                {
                                    position = i;//the position is captured here
                                    break;
                                }
                            }
                        }
                        str = str.Substring(position + 1);
                        sw.Write(str);
                    }
                    else
                        sw.Write(str + ",");
                    if (counter % 2 == 0) sw.WriteLine();

                }
                } while (str != null);
            sw.Close();
        }
    }


I hope it will help you.............If you face any problem you can further acknowlwdge me and if the code works plz inform me






Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
Save PDF file as text file in VB.Net kvenkatu Classic ASP Basics 0 April 7th, 2006 01:09 PM
save a PDF file as text file through VB. NET kvenkatu VB.NET 0 April 6th, 2006 12:15 PM
Extract text from text file & put in dropdown box tsukey Beginning PHP 5 July 20th, 2004 09:49 PM
Reformatting data pilmart Classic ASP Databases 2 April 22nd, 2004 07:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.