2020. 1. 25. 11:44ㆍ카테고리 없음
How to read and write random access records in.NET. Now I know random access files are dead and the response on every query I found with google/bing/yahoo searches was don't use random access use Binary serialization but I can not. I have a situation where I need to read and modify 256 byte records in a file that is used by another application.
Hi thereI am just starting process of converting a word vba script to open office basic script and am running into difficuly.When i try read a csv file into the writer using basic i get read exceeds EOF for lines past of file - its not stopping the loop. I'd using a loop structure where the EOF is tested at the bottom of the loop.
Much more reliable. Apparently the last line in the file isn't setting EOF for the next loop and so you get one extra attempt to fetch a line from a file that's actually at EOF.
I don't recall the particular circumstances when this happens because it's been a while since I've been in VB, but it has a LOOP.UNTIL structure, doesn't it? Try that and see if that works for you.Does OpenOffice support TextStreams? That might be another solution. The file is treated as an object; you don't have to worry about EOL's and EOF's and stuff like that.
Thought of something overnight. I see you're using OO on Windows. Windows uses a two-character End-of-Line marker. Wasn't OO developed first as a Unix app? Unix uses a one-character EOL. Could be the Line Input is reading up to the new-line character, leaving the file pointer still pointing to the second character, so it's not generating an EOL until the next Line Input.You could handle that with this loop structure:open file for inputread first linewhile not EOF-process the string-read the next lineloopclose fileSorry 'bout the format.
Editor doesn't take tabs. The two statements with the hyphens are inside the while loop. Hi,The problem is that your file is encoded as UTF-16 (Unicode), and Line Input normally handles 8-bits encoding of the operating system.
Himy 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.0i had read the text file by usingDim sfil As StringDim ddDim schunk As StringDim nSourceFile As Integer, sText As Stringsfil = 'E:proposed.txt'nSourceFile = FreeFileOpen sfil For Input As #nSourceFileDo While Not EOF(1)Line Input #1, schunkthen i had done the calculation. Now i want 2 write the result in the same text file.can anyone help me regarding this issue.regardsvivek. 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. Have a simular problem.so, suggest you open in APPEND-modeand 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.:).
Have a simular problem.so, suggest you open in APPEND-modeand 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. 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.
Random Access Vb6 Do While Not Eof Delete Record Player
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 nameThe 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.
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? 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.:). 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. 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?
Vivekfirs 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.Thankshimy 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.0i had read the text file by usingDim sfil As StringDim ddDim schunk As StringDim nSourceFile As Integer, sText As Stringsfil = 'E:proposed.txt'nSourceFile = FreeFileOpen sfil For Input As #nSourceFileDo While Not EOF(1)Line Input #1, schunkthen i had done the calculation. Now i want 2 write the result in the same text file.can anyone help me regarding this issue.regardsvivek.