dcsimg
Results 1 to 6 of 6
  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    14

    Open a TXT file for writing by 1 user, then allow other network users to read it.

    Hello;

    As the title says, I want to open a file for writing by 1 user (the first 1 to start the app), keep it open, but allow other users on the network to read it.

    I have tried every combination of OPEN commands and MODES I can think of, but no joy.

    I searched Google with variations of what I'm trying to do -- no luck.

    My current code is:

    iFileNo = FreeFile
    Open ThisLOCKFILE For Output Lock Write As #iFileNo '' open the file for writing
    MsgBox "File opened for writing, now try and open for read..."
    Open ThisLOCKFILE For Input Access Read As #23 '' open the file for reading

    but I get error 55 (File Already Open).

    Is what I want to do possible? and if so... how?

    Thanks.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,376

    Re: Open a TXT file for writing by 1 user, then allow other network users to read it.

    I'm not exactly sure what you're trying to do, but you do have to close it before others (or even you) can open it for reading.

    Good Luck,
    Elroy

    EDIT1: If you want multiple users (or even multiple instances in the same program, or same user) at the same time, use Open TheFile For Binary ...

    EDIT2: I haven't used it in a while, but I believe "Open TheFile For Random ..." would also work.

    EDIT3: Wilson, if you think about it, any ASCII/ANSI (or even Unicode) file that you open for either Input or Output is possibly going to be changing sizes each/every time you re-write it. Therefore, how is one user possibly going to know where things are when they're dynamically being changed by another user. That is just highly inadvisable even if you could manage a work-around (thinking API calls). You're much better off designing a file structure whereby you have specific places (byte offsets) for things, and then possibly update them when needed. This is precisely what the Random and/or Binary approach can do for you.

    Another alternative is to use the Windows INI API calls. These could certainly get the job done, but they're probably not as fast as opening a file for Binary or Random.

    EDIT4: I promise, this is my last edit. I'm just thinking that you may want a file that's Notepad-readable (i.e., ASCII/ANSI/Unicode). If this is the case, I'd just use Random, and then put a vbCrLf sequence as the last two bytes of each record, and also make sure all the data I wrote was ASCII (or ANSI or Unicode). If you go Unicode, you will need to deal with a bit of header information, but I'll let you ask before going into that (or it's easily Googlable).
    Last edited by Elroy; Jan 10th, 2018 at 06:19 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    14

    Re: Open a TXT file for writing by 1 user, then allow other network users to read it.

    I guess I didn't provide enough background info on what I'm wanting to accomplish.

    1) I want the first user to open the file and write the: 1) user name, 2) machine ID, 3) date, and 4) time to a .TXT file (1 value per line). The file will have only 4 lines and only 4 lines. I *think* I need to keep the file open so that other network users can not modify it.

    I then want other copies of the program (i.e., users on the network) to read the file and be able to identify the user/machine that opened the program (and wrote to the file).

    The code snippit I provided was just my "test" code trying to read the file after it was opened.

    So...

    EDIT 1 -- I will try.
    EDIT 2 -- I will try.
    EDIT 3 -- IMO is a non-issue as I will be reading 4 lines from the file (the file while changing size somewhat will not grow and grow and grow).
    EDIT 4 -- the file is an ascii txt file (as noted in the title). Hopefully the FOR RANDOM will work.

    Thanks for the info!!

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    14

    Re: Open a TXT file for writing by 1 user, then allow other network users to read it.

    Also... I have read the on-line help in the "MSDN Library Visual Studio 6.0". It discusses:

    mode Required.Keyword specifying the file mode: Append, Binary, Input, Output, or Random. If unspecified, the file is opened for Random access.

    access Optional. Keyword specifying the operations permitted on the open file: Read, Write, or Read Write.

    lock Optional. Keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.


    but does not provide any details on when/why to use the various selections.

  5. #5
    Fanatic Member
    Join Date
    Dec 2014
    Posts
    622

    Re: Open a TXT file for writing by 1 user, then allow other network users to read it.

    what you ask is not that people can write simultaneously, but that people can read a file and get info about it.
    of course its doable,
    first user create the text file with 4 lines using the print # command, and close it,
    then each user in the network simply open the text file using the line input # command and close it.

    now, first you create the gui, the user can write the 4 parameters, when done, you save it all immediately,
    you dont need to lock it, you only do that if you keep the file alive, adding data in real time.
    the same when the user open the file, you open, get the data and close it, no need to keep it open.

  6. #6
    Frenzied Member
    Join Date
    Dec 2008
    Posts
    1,118

    Re: Open a TXT file for writing by 1 user, then allow other network users to read it.

    I am using a Tree note program called RightNote. (I love it)
    I have the Pro version, but this should work with the free version
    Within any single file, you can have Tabs for different subjects.
    I have one Tab for ToDo and another for Clipboard.
    I type a list of To Do tasks in the ToDo Tab
    I paste text into the clipboard tab, that I wish to be able to copy on the other PCs'
    I have a 'Share' folder in my main PC, and the RightNote File is in that folder.
    On all the other PCs, I have mapped to that 'share' folder on my main PC.
    If two PCs have that file open, and one of the PCs alters anything, then RightNote prompts me (on the other PCs), to reload the file.

    I am now a happy chappy,
    Rob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width