By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,646 Members | 2,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,646 IT Pros & Developers. It's quick & easy.

write in text file thru vb 6.0

P: 11
hi
my requirement is read the data from the text file through vb. then calculate the data and write the result on the same text file where the data has retrived thru vb 6.0. how 2 implement the namespace in vb 6.0

i had read the text file by using

Dim sfil As String
Dim dd
Dim schunk As String
Dim nSourceFile As Integer, sText As String
sfil = "E:\proposed.txt"
nSourceFile = FreeFile

Open sfil For Input As #nSourceFile
Do While Not EOF(1)
Line Input #1, schunk


then i had done the calculation . now i want 2 write the result in the same text file.

can anyone help me regarding this issue.

regards
vivek
Nov 2 '06 #1
Share this Question
Share on Google+
11 Replies


Expert 5K+
P: 8,434
If you want to do this for more than one record, then I don't think a plain text file will work. You might have to go with a "random access" or "binary mode" text file. That is, one which allows you to read/write at specific points.

Look into the Binary and Random options on the Open statement. I'm about to leave work, so won't be with you for a while...

If there is only one record in the file, then the simplest thing might be just to Close it then Open it again before using Print # to write the result.
Nov 2 '06 #2

100+
P: 267
hi

you can't use print just like that because your in the input-mode.
once you closed your textfile and open this open it again in output-mode, you will lose all information.
so, suggest you open in APPEND-mode
and use Print # to place your data at the end of the existing data.
Nov 2 '06 #3

P: 3
have a simular problem...

>so, suggest you open in APPEND-mode
>and use Print # to place your data at the end of the existing data.

But how do you edit a file. How can you for example add data in the midle of a textfile?

Say you have a textfile with lots and lots of text. Somewhere in that text there is a word, say "october", and you want to ad a few lines right after that. One could of course remove all text after the word "october", and store it somewhere, Than apend the lines you want to add, and finally append the stored text at the end of the file again. But that isn't a nice solution. Especially for large files.

I read somthing in this tread about opening the file in binary mode or use random access-mode. But you cant edit a textfile in these modes, can you? Not if you want to be able to open it in a texteditor later. So how do you solve this?

This migth be a newbie question, so have patiance with me. :)
Nov 12 '06 #4

100+
P: 1,646
have a simular problem...

>so, suggest you open in APPEND-mode
>and use Print # to place your data at the end of the existing data.

But how do you edit a file. How can you for example add data in the midle of a textfile?

Say you have a textfile with lots and lots of text. Somewhere in that text there is a word, say "october", and you want to ad a few lines right after that. One could of course remove all text after the word "october", and store it somewhere, Than apend the lines you want to add, and finally append the stored text at the end of the file again. But that isn't a nice solution. Especially for large files.

I read somthing in this tread about opening the file in binary mode or use random access-mode. But you cant edit a textfile in these modes, can you? Not if you want to be able to open it in a texteditor later. So how do you solve this?

This migth be a newbie question, so have patiance with me. :)
Looks like you answered you own question. Sometimes the answer may not be as elegant as you would like.

Expand|Select|Wrap|Line Numbers
  1. stLeftText = Left(stAllText, lngInsertPoint)
  2. stLeftText = stLeftText & stTextToInsert
  3. stAllText = stLeftText & Right(stAllText, Len(stAllText) - lngInsertPoint)
  4.  
Nov 12 '06 #5

Expert 5K+
P: 8,434
I read somthing in this tread about opening the file in binary mode or use random access-mode. But you cant edit a textfile in these modes, can you? Not if you want to be able to open it in a texteditor later. So how do you solve this?
That's not strictly true. A text file is a text file. The different modes just allow you to manipulate it (or any type of file) in different ways.

Random is useful for working with something like a database, where you have records of (up to) a certain, specified length. You can read and write any specified record, without affecting the rest of the file.

Binary mode is probably the most powerful, allowing you to read and write any number of bytes to/from any point in the file. So for editing purposes, it could be quite useful. However, the problem you run into is when things change in length. If you wanted to change the word "CAT" to the word "DOG" for example, you could simply overwrite it in place. But if you wanted to change "CAT" to "WOMBAT", then all of the subsequent contents of the file would have to shift right three places to make room.

Normal Input/Output/Append mode is by far the simplest to use, but has a number of restrictions. It does, however, allow you to do things like the Line Input statement which inputs to the next line break, and so is more intuitive to use.

As willakawill pointed out, probably the simplest way to insert something in the middle of a text file is to read in the entire file, insert the text where you want it, then write the whole thing out again. Depending on how important the data is, you may or may not want to take precautions such as making a backup copy. This kind of procedure is performed more often than you might think - take most ZIP utilities, for example. When adding to an existing archive, they will typically do something like:
  • copy it to a temporary file
  • append new data to the temp file
  • delete the original file
  • rename the new file to the original name
The point of all this, of course, is that it minimises the chance of losing your original data if anything goes wrong along the way.

This migth be a newbie question, so have patiance with me. :)
Without newbies, this place would be no fun at all.
Nov 12 '06 #6

P: 3
Hello and thanks for your suggestions people!!

The awnser about storing in records seemed interesting. If I understood correctly its possible to store data anywhere inside a textfile, without corrupting the file. I have looked around for this on the web and it appears a bit complicated and hard to understand. Will look into it more later. Might learn someting even if it does not work well in this case. Anyway, thanks for the tip.

Have a question about the simplier way to solve the problem. (When you just read the entire file, add the new data, and then put it all back again). How much data can you store in a string variable? We're talking abaout thousends of lines here. I remeber playing a litle with perl and shellscripting a few years ago. Than I processed a large amount of files with a script, and the files ended up concatenated. Is there a risk for this to happend, if I use the code suggested by willakawill for example?
Nov 14 '06 #7

Expert 5K+
P: 8,434
Hello and thanks for your suggestions people!!
... Might learn someting even if it does not work well in this case. Anyway, thanks for the tip.
That's the spirit! :)

Have a question about the simplier way to solve the problem. (When you just read the entire file, add the new data, and then put it all back again). How much data can you store in a string variable? We're talking abaout thousends of lines here. I remeber playing a litle with perl and shellscripting a few years ago. Than I processed a large amount of files with a script, and the files ended up concatenated. Is there a risk for this to happend, if I use the code suggested by willakawill for example?
The files will only end up concatenated if you explicitly do so. As I often say to people at work, possibly the biggest problem with computers is that they do exactly what you tell them to do, not what you want them to do.

If willakawill doesn't beat me to it, and you still need help, I'll try to get back to you at lunch time (it's mid-morning here now) or after work. Can't spare more time at the moment.

With this sort of thing though, the best way to learn is just to try out everything you can think of, and see what happens. As long as it's on unimportant data, of course. :)
Nov 14 '06 #8

Expert 5K+
P: 8,434
Sorry, forgot to mention. Unless you're talking about really huge data volumes, there's no real restriction on string sizes these days.

The upper limit is probably your available RAM (physical or virtual, not sure). And if you don't have room for it all in there, just read and write a line (or bigger chunk) at a time.
Nov 14 '06 #9

P: 3
Sorry, forgot to mention. Unless you're talking about really huge data volumes, there's no real restriction on string sizes these days.

The upper limit is probably your available RAM (physical or virtual, not sure). And if you don't have room for it all in there, just read and write a line (or bigger chunk) at a time.
I guess there is no problem to store a few hundred kilobytes of text in a string variable then. Problem solved. :)

.... Than I processed a large amount of files with a script, and the files ended up concatenated. Is there a risk for this to happend, if I use the code suggested by willakawill for example?
I realize now that I wrote concatenated, when I really ment truncated. :\ Bet it makes more sense now. :)

Anyway, Im glad you people took time to look at my problems. Great forum.
Nov 16 '06 #10

Expert 5K+
P: 8,434
I guess there is no problem to store a few hundred kilobytes of text in a string variable then. Problem solved. :)
Not unless you're using a really old computer, I'd say.

I realize now that I wrote concatenated, when I really ment truncated. :\ Bet it makes more sense now.
Ah, concatenated, truncated, same thing. :)

Got to watch those little details in programming, or they'll bite you.

Anyway, Im glad you people took time to look at my problems. Great forum.
It's been fun. So, you've got it sorted now?
Nov 16 '06 #11

P: 1
Hello Mr. vivek

firs close the text file and as you had opened the text file for input. now open the text file as output and use print command to print the lines to text file.

In case of prob pl write back i will send u my code of generating the text file.

Can u help me in other case which i facing the prob.

I want to print the existing text file to printer ( either local or network) thru VB coding. User has to input the file name or i can make different options for different files in VB menu option.

Thanks



hi
my requirement is read the data from the text file through vb. then calculate the data and write the result on the same text file where the data has retrived thru vb 6.0. how 2 implement the namespace in vb 6.0

i had read the text file by using

Dim sfil As String
Dim dd
Dim schunk As String
Dim nSourceFile As Integer, sText As String
sfil = "E:\proposed.txt"
nSourceFile = FreeFile

Open sfil For Input As #nSourceFile
Do While Not EOF(1)
Line Input #1, schunk


then i had done the calculation . now i want 2 write the result in the same text file.

can anyone help me regarding this issue.

regards
vivek
Sep 11 '07 #12

Post your reply

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