Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

writing and reading from file

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
laukhengsiong
Asked:
laukhengsiong
  • 8
  • 7
  • 6
  • +2
1 Solution
 
aikimarkCommented:
this is a duplicate question.

Please ask Community Support to delete it.
0
 
rspahitzCommented:
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
 
laukhengsiongAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
rspahitzCommented:
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
 
laukhengsiongAuthor Commented:
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
 
rspahitzCommented:
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
 
laukhengsiongAuthor Commented:
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
 
rspahitzCommented:
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
 
laukhengsiongAuthor Commented:
So any solution for overwriting the existing line where the previous version is??? Any sample codes??? Urgently!!! Thanks
0
 
aikimarkCommented:
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
 
laukhengsiongAuthor Commented:
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
 
aikimarkCommented:
If this is a binary file, you CAN'T read this on a line-by-line fashion.
0
 
laukhengsiongAuthor Commented:
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
 
aikimarkCommented:
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
 
laukhengsiongAuthor Commented:
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
 
aikimarkCommented:
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
 
laukhengsiongAuthor Commented:
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
 
aikimarkCommented:
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
 
rspahitzCommented:
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
 
rspahitzCommented:
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
 
aikimarkCommented:
And there is the INI format, which provides direct access to the keys and values. (see my earlier comment)
0
 
DanRollinsCommented:
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
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 8
  • 7
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now