Results 1 to 16 of 16

Thread: cannot save to text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2010
    Posts
    377

    cannot save to text file

    I have this program that extract a text file.

    Later with the same program, I open this text file for reading and editing and then save what was edited.

    But when I try to save it, the program tells the file is open. What could be the problem?

    But when I delete this extracted text file and replace it manually with my backup same text file for the same purpose, I can save successfully.

    I guess the text file is extracted in somewhat way that cannot be used for editing and later for saving.

    It must be noted that I have already tried to restart the computer and tried saving but still the program says the the file is already open.

    The file is set to achive properties in windows.
    Last edited by iamresearcher; Nov 22nd, 2011 at 08:31 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: cannot save to text file

    The problem could be that the file is open.

    If you posted the code you are using then I wouldn't need to ask this, but how are you "opening" the file?
    How do you make changes?
    How are you trying to save it again?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2010
    Posts
    377

    Re: cannot save to text file

    Hack,

    Like I said, when that certain file is extract from my program and I read it, I cant do a modification and save it.

    But that certain file is not coming from my program, like I copy and paste it there on the application path from my usb or somewhere, I can read, modify and save it.

    And even if I restart my computer, so that means if my program is still doing something on that file, it will be stopped from doing so since I have restarted my computer. But when I read, modify and save, it is still failure.

    So I guess there is something on the extraction that makes it read-only file something like that but when I check the windows file property of that file, it is set to archive.

    What could it be?
    Last edited by iamresearcher; Nov 25th, 2011 at 08:46 AM.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: cannot save to text file

    It sounds like you should read the article Where should I store the files that my program uses/creates? from our Classic VB FAQs (in the FAQ forum)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2010
    Posts
    377

    Re: cannot save to text file

    si_the_geek,

    thanks for your response. but that file is settings file of the program that needs to be read for proper execution of the program. if can be modified thru the program if there is a need to change some settings.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: cannot save to text file

    So what? The same rules apply no matter what the purpose of the file is.

    Simply moving the file to a valid location is very likely to solve your problem.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2010
    Posts
    377

    Re: cannot save to text file

    while it is possibly good to be on the valid location but the program is suppose to be portable, thus the setting must always accompanying the software.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: cannot save to text file

    It isn't just "good" to use a valid location, it is mandatory if you want your program to work.

    Some locations not listed in the article (such as on a USB drive) are valid if security of the folder is set appropriately, but there are plenty of locations that aren't (such as the Program Files folders, the root of a hard drive, non-writeable DVDs, etc).


    Try temporarily using a valid location to see if it stops the problem. If it does, you can then think about how to pick a valid location at run time.

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: cannot save to text file

    Quote Originally Posted by iamresearcher View Post
    but that file is settings file of the program that needs to be read for proper execution of the program. if can be modified thru the program if there is a need to change some settings.
    So in your program you read the settings file and later on try to write to it. Sounds like you haven't closed it before trying to update it.

    Perhaps if you posted the code you are using it would help.

  10. #10
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: cannot save to text file

    Can you post your code, searcher? So that we can have a better look on your problem.
    If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.

    If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.

    For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2010
    Posts
    377

    Re: cannot save to text file

    Code:
    Option Explicit
    
    Public Function ReadIniValue(INIpath As String, Key As String, Variable As String) As String
    On Error GoTo MyError
    
    Dim NF As Integer
    Dim Temp As String
    Dim LcaseTemp As String
    Dim ReadyToRead As Boolean
        
    AssignVariables:
            NF = FreeFile
            ReadIniValue = ""
            Key = "[" & LCase$(Key) & "]"
            Variable = LCase$(Variable)
        
    EnsureFileExists:
        Open INIpath For Binary As NF
        Close NF
        SetAttr INIpath, vbArchive
        
    LoadFile:
        Open INIpath For Input As NF
        While Not EOF(NF)
        Line Input #NF, Temp
        LcaseTemp = LCase$(Temp)
        If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
        If LcaseTemp = Key Then ReadyToRead = True
        If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
            If InStr(LcaseTemp, Variable & "=") = 1 Then
                ReadIniValue = Mid$(Temp, 1 + Len(Variable & "="))
                Close NF: Exit Function
                End If
            End If
        Wend
        Close NF
    Exit Function
    MyError:
    
    Exit Function
        
        Resume Next
    End Function

    Code:
    Public Function WriteIniValue(INIpath As String, PutKey As String, PutVariable As String, PutValue As String)
    Dim Temp As String
    Dim LcaseTemp As String
    Dim ReadKey As String
    Dim ReadVariable As String
    Dim LOKEY As Integer
    Dim HIKEY As Integer
    Dim KEYLEN As Integer
    Dim VAR As Integer
    Dim VARENDOFLINE As Integer
    Dim NF As Integer
    Dim X As Integer
    
    AssignVariables:
        NF = FreeFile
        ReadKey = vbCrLf & "[" & LCase$(PutKey) & "]" & Chr$(13)
        KEYLEN = Len(ReadKey)
        ReadVariable = Chr$(10) & LCase$(PutVariable) & "="
            
    EnsureFileExists:
        Open INIpath For Binary As NF
        Close NF
        SetAttr INIpath, vbArchive
        
    LoadFile:
        Open INIpath For Input As NF
        Temp = Input$(LOF(NF), NF)
        Temp = vbCrLf & Temp & "[]"
        Close NF
        LcaseTemp = LCase$(Temp)
        
    LogicMenu:
        LOKEY = InStr(LcaseTemp, ReadKey)
        If LOKEY = 0 Then GoTo AddKey:
        HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
        VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
        If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable:
        GoTo RenewVariable:
        
    AddKey:
            Temp = Left$(Temp, Len(Temp) - 2)
            Temp = Temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
            GoTo TrimFinalString:
            
    AddVariable:
            Temp = Left$(Temp, Len(Temp) - 2)
            Temp = Left$(Temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid$(Temp, LOKEY + KEYLEN + 1)
            GoTo TrimFinalString:
            
    RenewVariable:
            Temp = Left$(Temp, Len(Temp) - 2)
            VARENDOFLINE = InStr(VAR, Temp, Chr$(13))
            Temp = Left$(Temp, VAR) & PutVariable & "=" & PutValue & Mid$(Temp, VARENDOFLINE)
            GoTo TrimFinalString:
    
    TrimFinalString:
            Temp = Mid$(Temp, 2)
            Do Until InStr(Temp, vbCrLf & vbCrLf & vbCrLf) = 0
            Temp = Replace(Temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
            Loop
        
            Do Until Right$(Temp, 1) > Chr$(13)
            Temp = Left$(Temp, Len(Temp) - 1)
            Loop
        
            Do Until Left$(Temp, 1) > Chr$(13)
            Temp = Mid$(Temp, 2)
            Loop
        
    OutputAmendedINIFile:
            Open INIpath For Output As NF
            Print #NF, Temp
            Close NF
        
    End Function
    Reading file:

    ReadIniValue(App.Path & "\config.txt", "Section1", "Labor")


    Write to file:

    WriteIniValue App.Path & "\config.txt", "Section1", "Labor", Rule1


    As you can see, the readinivalue and writeinivalue it opens and closes the connection on each use of the function.

  12. #12
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: cannot save to text file

    Are you trying to save data to a text file but using the same format as ini?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2010
    Posts
    377

    Re: cannot save to text file

    yes.

  14. #14
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: cannot save to text file

    Try Doogle's code here.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2010
    Posts
    377

    Re: cannot save to text file

    I have tried that already. and still same error.

  16. #16
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: cannot save to text file

    In your ReadIniValue routine, if you successfully open the file after "Open INIpath For Input As NF" and an error occurs, your routine jumps to the MyError label and exits the function. The file is never closed in this case. Suggestions:

    1) Whenever you get to a line where you close the file, reset the file number to zero after closing, i.e:
    Code:
       Close NF: NF=0
    2) In your error handler check to see if NF is zero & if not, then close it, i.e:
    Code:
    MyError:
      If NF <> 0 Then Close NF
    Don't know if that is the exact problem or not, but it is a potential problem nonetheless

    Also, you have no error handling in your WriteIniValue function. A similar situation can occur where an error occurs and the routine exits with a file still opened. Suggest adding error checking there too
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

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



Click Here to Expand Forum to Full Width