473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to update a single line of text file

i have a text file which has a fixed length lines. How can I update a particular line (record ) basing on a record ID provided? below is my code which retrieves a particular line from the textfile & display it in xtboxes in my update form.:

Expand|Select|Wrap|Line Numbers
  1. If System.IO.File.Exists(FILE_NAME) = True Then
  2. Dim fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
  3. Dim sr As New StreamReader(fs)
  4. Dim Contents As String = sr.ReadToEnd()
  5. Dim strSearch As String = "^(.*?)" & Quest.Text & ""
  6. Dim myRegex As New Regex(strSearch, RegexOptions.Multiline)
  7. If myRegex.IsMatch(Contents) Then
  8. Dim myMatch As Match = myRegex.Match(Contents)
  9. Dim theFoundLine As String = myMatch.Groups(0).Value
  10. 'Display the found line
  11.  
  12. Dim splitText() As String = theFoundLine.Split(";")
  13. OS01Rec.Text = splitText(0).Trim
  14. os01a.Text = splitText(1).Trim
  15. OS01b.Text = splitText(2).Trim
  16. OS01c.Text = splitText(3).Trim
  17. ...................... so on
so basically this array holds each field in its respective textbox. Now how to overwrite the textfile???any idea?
Thanks.
Sep 3 '08 #1
5 2300
Curtis Rutland
3,256 Expert 2GB
Basically you should do the same thing that you did reading from it, but using StreamWriter instead of StreamReader.

First of all, make sure you close all open streams when you are done with them, so close your FileStream and StreamReader objects (you should do this as soon as you are done reading from them). When you want to write, create the FileStream again, except with Write permissions instead of Read. Then, make a StreamWriter with that FileStream. Then, use the StreamWriter to write to the file.
Example
Expand|Select|Wrap|Line Numbers
  1. FileStream fs = new FileStream(@"c:\dev\test.txt",FileMode.Open, FileMode.Write);
  2. StreamWriter sw = new StreamWriter(fs);
  3. sw.WriteLine("Whatever I want to write to the file.");
  4. sw.Close();
  5. fs.Close();
  6.  
Sep 3 '08 #2
Example
Expand|Select|Wrap|Line Numbers
  1. FileStream fs = new FileStream(@"c:\dev\test.txt",FileMode.Open, FileMode.Write);
  2. StreamWriter sw = new StreamWriter(fs);
  3. sw.WriteLine("Whatever I want to write to the file.");
  4. sw.Close();
  5. fs.Close();
  6.  
[/quote]

Thanks for your reply InsertAlias.
But I want only that particular line of my text file to be updated,which was called in the form for updation.This code is overwriting the first line of my text file.It doesnot update that particular ID record . I mean i am getting duplicate lines in my text file (with same ID numbers). Please see below the actual sample of text file:

4;dhl;khush;delivered;22;33;44urban;kashif;malik;r ehman;;;good;n;;1;2;3;;5;6;7;8;;0;11;22;33;;55;004
3;;multiple;No;done;22;33;44urban;kashif;malik;reh man;Y;N;good;n;;1;2;3;4;5;6;7;22;22;2;003
2;;multiple;No;done;22;33;44urban;kashif;malik;reh man;Y;N;good;n;;1;2;3;4;5;6;7;22;22;2;002

The last record is the ID against which we select that line for updation. so only that ID should be updated not others.
Any thoughts? Would be grateful foryour reply.Thanks.
Sep 4 '08 #3
Anyone please. I have to solve this urgently. Thanks in advance for your help.
Sep 4 '08 #4
Curtis Rutland
3,256 Expert 2GB
Well, the way I would solve the problem is to overwrite the whole text file. I'd re-write the things that didn't change as well as what did.

That's one of the problems with using text files instead of databases...less flexibility.
Sep 4 '08 #5
Well, the way I would solve the problem is to overwrite the whole text file. I'd re-write the things that didn't change as well as what did.

That's one of the problems with using text files instead of databases...less flexibility.
Boss, How to overwrite the whole file? can you please help me with some code. Thanks.
Sep 4 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old...
4
by: shank | last post by:
Visually, the page will look somewhat like a spreadsheet. It could have hundreds of records (rows) displayed. I want to enable the user to edit any one or any number of records and any fields, then...
4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
0
by: Support | last post by:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head>...
1
by: thangchan | last post by:
Hi all, i am getting SQL update problem. as below ======================error messages ======================= Server Error in '/CMS' Application....
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
5
by: =?Utf-8?B?RGF2aWRF?= | last post by:
Hi, I have to open a text file in c# and read few lines and then I need to update some lines in the middle of the file. I can read and write by using the streamReader or streamWriter but I don't...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.