Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi people..!

I´d like to know if somebody could help me with a problem here.

So, I´ve a .txt file and it has around 5000 lines full of numbers, and I need writing in certains lines some values, in that lines I need reading certains positions/columns, and later writing other values at the same place where that I took of the other...I´ll try to be a little more clear ok:

Example of my .txt file

98489654644564564546546546655645664

9849849844849849489

984984998489498494894948

and in that way, till the end of document...

so, you see the bold numbers ? that is the ones that I need read and later write other values at the same position, I think it would be something like an "update", but in a .txt file...:D

So, I can read the entire document using the streamreader, it would be something like....

Dim arquivoTexto As String
Dim leitor As StreamReader = File.OpenText(nomeArquivo)

arquivoTexto = leitor.ReadToEnd

however, I´d like to know if there´s a way to read just certains lines from my .txt file and not the whole file, something that works like a index or kinda of that, cause the lines that I need to read will be always the same, the only thing that will change will be the values, So, if I can get the line that I want, I can take the numbers I want through a simple subString right... ?

my problem is....take that lines !!!

the second part of my problem is: how to write in .txt, the numbers I changed at the same positions where the other have been...

example:
original .txt file line 0 = 123456798

.txt file after alteration line 0 = 123499798

summarizing, I know exactly the lines and columns where the numbers that I need to modify will be, I just dont know how to get that lines/colums, and dont know too how to write the new numbers at the right positions.... :/

*** I never will need writing 2 numbers and then write 3 for example, if I read 2, just 2 will be modified...

Wow, I hope it was the more possible clearing...really I did try ;)

sorry for any english mistakes.....I´m brazilian guy, trying to learn english language :)

Since, thanks any help

hugs 4 all
Posted
Comments
Sergey Alexandrovich Kryukov 15-Sep-13 11:51am    
By the way, if you add proper capitalization, a bit more accurate punctuation (blank spaces after punctuation characters), and also get rid of "textspeack" (such as 4 for "for", which looks trendy" but is not very welcome on this site where people value accuracy), I think you progress in English will look impressive.

Cheers,
—SA

1 solution

There is no function that will help you: text files have no structure, the "lines" are just a character (or two depending on the system) inserted into the character stream which is treated as a line termination character by applications.
So in order to find "line 123", you have to read all the text up to that point and count the number of new lines.
If you want to change a character or characters in a file, then that is one thing - you can SEEK the stream to the appropriate position and write the new values, but if at any point you want to write a longer or shorter string then you have to read the whole file up to that point, writing it to a second file, write your new data, then copy the rest of the input file to the output.

The easiest way to do what you want is probably to use File.ReadAllLines which returns an array of strings, one for each line in the file, modify the strings, and then use File.WriteAllLines to regenerate the file when you are done.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Sep-13 11:53am    
Sure, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900