473,698 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to update a single line of text file

91 New Member
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 2324
Curtis Rutland
3,256 Recognized Expert Specialist
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
kashif73
91 New Member
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;del ivered;22;33;44 urban;kashif;ma lik;rehman;;;go od;n;;1;2;3;;5; 6;7;8;;0;11;22; 33;;55;004
3;;multiple;No; done;22;33;44ur ban;kashif;mali k;rehman;Y;N;go od;n;;1;2;3;4;5 ;6;7;22;22;2;003
2;;multiple;No; done;22;33;44ur ban;kashif;mali k;rehman;Y;N;go od;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
kashif73
91 New Member
Anyone please. I have to solve this urgently. Thanks in advance for your help.
Sep 4 '08 #4
Curtis Rutland
3,256 Recognized Expert Specialist
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...les s flexibility.
Sep 4 '08 #5
kashif73
91 New Member
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...les s 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
3267
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 scripts working on a new server with the most recent version of PHP. I've pretty much taken care of all the various errors that were popping up. Most only pointed out out non-fatal undefined or assumed variables. I've been able to cure most of...
4
6136
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 click a save button to UPDATE the SQL table. I'd like to use stored procedures if possible. How is this done? Where do I start? thanks
4
2776
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
1751
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> <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> <meta name=ProgId content=Word.Document> <meta name=Generator content="Microsoft Word 11"> <meta name=Originator content="Microsoft Word 11">
1
4967
by: thangchan | last post by:
Hi all, i am getting SQL update problem. as below ======================error messages ======================= Server Error in '/CMS' Application. -------------------------------------------------------------------------------- 無值提供給一或多個必要參數。 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more
2
3106
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 havnt tested the index yet ) so you can use an .UPDATE( dataTable ) on the data adapter. Otherwise you will get an exception error. Is this statement true? ---- Now me fumbling thru
5
18976
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 know how to read for example five lines and then update the sixth line and continou to read few lines, update few etc. Thanks David
3
3960
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
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.