Solved

writing and reading from file

Posted on 2002-05-13
23
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 6
  • +2
23 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 7005821
this is a duplicate question.

Please ask Community Support to delete it.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7006692
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
ID: 7007403
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 22

Expert Comment

by:rspahitz
ID: 7008850
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
ID: 7009962
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
ID: 7011437
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
ID: 7013027
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
ID: 7014182
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
ID: 7020576
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
ID: 7020669
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
ID: 7022867
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
 
LVL 45

Expert Comment

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

Author Comment

by:laukhengsiong
ID: 7023290
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
ID: 7023988
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
ID: 7026038
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
ID: 7027137
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
ID: 7038664
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
ID: 7039265
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
ID: 7039705
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
ID: 7039716
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
ID: 7040206
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
ID: 7765333
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
ID: 7837598
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

732 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