Results 1 to 4 of 4

Thread: File Handling

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63

    File Handling

    er ... how do I open a exsit file and write extra string into any line(s) in the original file

    like say the original file is

    ABCDHIJKLMN...

    and i wanna insert EFG into ABCD and HIJ...

    what should i do? :s

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I haven't done this, but you are going to want to look into streams. Maybe TextWriter and TextReader streams?..?
    Not sure, good luck.

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    I played around for you, and I couldn't get it to insert data, just overwrite...

    Code:
    	// C:\text.txt contains "ABCDEFGMNOPQRSTUVWXYZ"
    
    	FileStream f = new FileStream("C:\\text.txt", FileMode.Open , FileAccess.ReadWrite);
    	while(f.ReadByte() != 'G')
    	{
    	}
    
    	f.WriteByte((byte)'H');
    	f.WriteByte((byte)'I');
    	f.WriteByte((byte)'J');
    	f.WriteByte((byte)'K');
    	f.WriteByte((byte)'L');
    				
    	f.Close();
    	// C:\text.txt contains "ABCDEFGHIJKLRSTUVWXYZ"
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I fear it can't be done. You must read in everything after the location where you want to insert, then write out what you want to insert and then the rest of the file.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width