Solved

writing and reading from file

Posted on 2002-05-13
23
216 Views
Last Modified: 2010-05-02
Adobe Photoshopv_1
Macromedia Freehandv_3
Macromedia Fireworkv_2
Microsoft Word97

FileNum=freefile()
I have this few names. When i read it using Open "c"\product.dat" for input as #FileNum,
the first word Adobe Photoshopv_1 have something like this: "ybAdobe Photoshopv_1". Why is this so???
i suspect this to be linefeed or something like that. how to get rid??? Adobe Photoshopv_1 is the
first name on the product.dat file.

Another problem is when i tried to UPDATE Macromedia Freehandv_3 to Macromedia Freehandv_4,
it becomes like this:
Adobe Photoshopv_1
Macromedia Freehand‰C⁁—ð¶Ø°T⸲⸶v_4romedia Fireworkv_2
Microsoft Word97

The line Macromedia Freehandv_4 is somehow updated but there is lot of ⸶ char and
part of the word Macromedia Firewordv_2 is lost somehow.

The code is like this:
Dim sEmptyStr, sPosition

versionUpdate= "Macromedia Freehandv_4"
nameoffile = "Macromedia Freehand"

FileNum = FreeFile()
    Open "C:\product.dat" For Input As #FileNum
    Do Until EOF(FileNum)
      Line Input #FileNum, newline
     
      test = InStr(newline, nameoffile)

      'get the position of the line
       if test=true
       sPosition = Seek(FileNum) - Len(newline) - 2
       end if

    Loop

Close #FileNum

sEmptyStr = Space(Len(versionUpdate)) 'Line's length

Open "C:\product.dat" For Binary Access Write As #FileNum
      Put #1, sPosition, sEmptyStr
      Put #FileNum, sPosition, nameUpdate
Close #FileNum
0
Comment
Question by:laukhengsiong
  • 8
  • 7
  • 6
  • +2
23 Comments
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
this is a duplicate question.

Please ask Community Support to delete it.
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Actually, you can delete http://www.experts-exchange.com/visualbasic/Q.20299943.html on your own if you get to it before any comments are added.

--
Open "c"\product.dat" for input as #FileNum

This will open the file, which contains binary encoding.  Whatever is in the file is probably required for the file to be properly read by the host application.  In you case, Photoshop apparently encodes its files starting with "ybAdobe Photoshopv_1" which helps it recognize that it is a valid Photoshop file.  If you change it, Photoshop will claim that it is an invalid file or the format is unrecognized.

If you're trying to read the image portion of a Photoshop file, you'll need to either:

1) Open Photoshop and export the file as a standard image file (.bmp, .gif, .jpg)

-or-

2) Load a Photoshop reference library into VB so that you can use VB to manipulate the file as a Photoshop file.  (And I don't know if such a library exists.)

0
 

Author Comment

by:laukhengsiong
Comment Utility
I'm trying to do a updating of files. Photoshop is just a name. I'm checking whether the file is old version against a new version.
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
There's no problem reading those files to determine the version (if you can figure out the encoding,) but changing it and writing it back without knowing the encoding is just about guaranteeing that the file will become corrupt (according to the primary associated application.)

0
 

Author Comment

by:laukhengsiong
Comment Utility
i think u misinterpreted my question. Actually the text file contains information of programs that i want to update. So when i updated the program, the text file will be read and respective line where the program is will be updated. Thanks
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Hi.  Let me see if I understand.  You have a textfile which holds information about other files.  Something updates this file whenever other files are changed.  When you load this textfile through VB, it contains some weird characters.

After further observing your code, I think the problem is that you are using incompatible read/write combinations.

You're reading line information (Line Input) but you're writing encoded binary (Put #1).

In your case, I'd probably recommend the simple line write:

' Read (same as current)

' Write
FileNum = FreeFile()
   Open "C:\product.dat" For append As #FileNum
   print #FileNum,
Close #FileNum

--
' Actually, the above is probably not exactly what you want, but if you can't figure it out, let me know and we can discuss a strategy, such as get/put combinations or even ADO since you're really creating a database.
0
 

Author Comment

by:laukhengsiong
Comment Utility
the reason why i used Put#1 is becos i use the sPosition variable to search for the
starting position of the existing file in the product.dat and then i copy it over(erasing
the old file) after verifying that it is the old version.
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Well, Put is normally associated with random access textfiles, which is why you're getting all of those strange characters.  When put inserts values it must be compressing them.  For example, if you put the number 65 it would take only one byte and have a hex value of 41, and appear in a text editor as a capital A.  When you try to read it back using Line Input, you don't get two characters, "6" and "5", but rather get a single character which must be interpreted as the number 65.  This gets more complex with numbers over 255 because then you get two bytes which have to be combined in order to get the numbers.

I think what you probably want is to change your input section to loop through records rather than lines.  Try looking at the help files for "get" relative to random access files.

0
 

Author Comment

by:laukhengsiong
Comment Utility
So any solution for overwriting the existing line where the previous version is??? Any sample codes??? Urgently!!! Thanks
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
If you don't change the length of the strings you are replacing, you can do the following:
1. Read the entire contents of the file into a string variable with the Input() function.
2. Replace the string(s) with the Mid$() function.
3. Write the entire contents with a Print ; statement.
0
 

Author Comment

by:laukhengsiong
Comment Utility
other than this solution, is there any more where u can read line by line and then replace that line with an updated version of the file??? Hope to find a better solution.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 45

Expert Comment

by:aikimark
Comment Utility
If this is a binary file, you CAN'T read this on a line-by-line fashion.
0
 

Author Comment

by:laukhengsiong
Comment Utility
So you mean i can change it to a other type of files.. like Open "C:\product.dat" For Input As #FileNum for reading then Open "C:\product.dat" For Output As #FileNum for writing???
So that i can read line by line???Got any sample code where i can read and then overwrite the existing files not using binary file????



0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
You have a file that wasn't created a line at a time.  Its data isn't arranged in lines.  Therefore, you can't read it a line at a time.  It doesn't matter what name you give it or how you open it.  It should be read in a similar fashion to the way it was written.
0
 

Author Comment

by:laukhengsiong
Comment Utility
I think u are mistaken... The file is created by me. I write the versions of the files that reside in user pc onto the file. Therefore, whenever user click on the prg, it will call on this file and the data is read. The prg will check the new version setup.exe in the network drive and compare with the user version. Then it will tell them that the version in their pc is old. Thanks for your attention
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Since you have control over the format of the file, I suggest you do any of the following:
1. Use the GetPrivateProfileString and PutPrivateProfileString API calls and format your program/version data into an INI format.

2. Use
Open...For Output ...
Print #xxx, yyyy
statements to create the file.
When the program starts, open this file for input, reading the lines of the file with the
Line Input #aaa, bbb
statement.  If you need to change the contents of this file, use the Open and Print statements mentioned at the start of comment 2.

3. Switch to a fixed-length record format and stick with the current
Open ... Binary ...
Put ...
Statements for accessing the contents.  The length of the record needs to accomodate the largest program name and version number.
0
 

Author Comment

by:laukhengsiong
Comment Utility
i tried to use the method two you mentioned but the problem is how should i write the code such that it knows the CURRENT line and then updated accordingly.... remember i use open binary because i can overwrite the line where the file is, and which contains the updated file
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
As I said earlier, unless you have fixed-length records, you will be unable to process the file in binary record fashion.

===========================
GOOD ADVICE:
"If what you are doing doesn't work, do something else."

PARAPHRASED (from a former sensei urging us to change)
"Even the dumbest lab rat will turn when they bump their head into a maze wall."
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Related note...

When you create a random-access database, as you are trying to do, you have to include many more things that allow for its growth and contraction.

Given the method that you are trying to use, the only practical way to build this is to re-write the entire file each time you change a record.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
Comment Utility
ummm...an alternate way would be to create, in essence, a table of contents (TOC) block with pointers to sections of the textfile where the content exists, then always simply append all data to the textfile and update the TOC using put statements to reference the starting/ending location of this block.

If the block changes, simply append a new block, update the TOC, and ignore the old block which is now invalid.

Since this method will produce a continusuously growing file, you will probably need a compaction routine that goes through and re-build the text file based on only the valid links in the textfile.

In essence, you'll be creating your own Access DB Engine (minus the SQL support)!
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
And there is the INI format, which provides direct access to the keys and values. (see my earlier comment)
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Hi laukhengsiong,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept rspahitz's comment(s) as an answer.

laukhengsiong, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now