Results 1 to 6 of 6

Thread: [RESOLVED] Text file stuff

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Resolved [RESOLVED] Text file stuff

    Hey everyone

    Does anyone know how to open a file and add the "%" at the beginning and the end of a file.

    Thanks
    Reston

  2. #2
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Re: Text file stuff

    I would simply say to read the file and then write it back.
    You can either read it all into a variable then write it back to the same file or open the file (open "MYFILE.txt" for input as #1) and another temp file (open "MyTempFile.txt" for output as #2) and then read from one and write to the other. Then delete the old one (Kill) and rename the new one to the old one.

    1)
    Code:
    Open "MyFile.txt" for input as #1
    
    Do
     Line input #1, ReadNewLine
     MyString = Mystring & ReadNewLine
    loop until EOF(1)
    
    close #1
    You now have the whole file in MyString (string variable).
    Just open the file again and write

    Code:
    Open "MyFile.txt" for output as #1
    ? #1, "%"
     ? #1, MyString
    ? #1, "%"
    close #1
    2)
    Code:
    Open "MyFile.txt" for input as #1
    Open "MyTempFile.txt" for output as #2
    
    ? #2, "%"
    
    Do
      Line Input #1, ReadNewLine
      ? #2, ReadNewLine
    loop until EOF(1)
    
    ? #2, "%"
    
    Close #1
    Close #2
    
    Kill "MyFile.txt"
    Name "MyTempFile.txt" as "MyFile.txt"
    I didn't include the paths, you would have to include paths or it will write to the same path as you project (I think)... Even if you want it to write to your project path it is still better to use App.Path & "\MyFile.txt"


    Mike

  3. #3
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: Text file stuff

    Okay you need the code to look something like this...
    Open "MyFile.txt" for output as #1
    Print #1
    Close #1

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: Text file stuff

    That works pretty good but when vb prints back to the file it's all on one line.
    The input looks like this below.I need the file to be preserved like it was.

    Thanks
    Reston

    O0000
    M00
    ( Menu File: W.120 )
    ( 12-05-2006 19:00:41 )
    N 1 G01G90G49G40G80G17
    N 2 G91G28Z0
    N 3 G91G28X0Y0A0.
    N 4 T 12M06
    N 5 G92X 119450 Y 151150 Z 229765 A- 80
    N 6 G01G90Y 0 F 2000000
    N 7 S 2700 M03
    N 8 M08
    N 9 G90A0.F 2000000

  5. #5
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Re: Text file stuff

    If you are using Print #1, [VARIABLE] (same as ? #1) then it shouldn't be one line.
    The Print # ends each line.
    Make sure your using Line Input # to get it.

    I pasted your txt sample into a MyFile.txt file.
    Then ran this code....
    Code:
    Open "MyFile.txt" For Input As #1
    Open "MyTempFile.txt" For Output As #2
    
    
    Print #2, "%"
    
    Do
    
      Line Input #1, ReadLine
      Print #2, ReadLine
    
    Loop Until EOF(1)
    
    Print #2, "%"
    
    Close #1
    Close #2
    
    Kill "MyFile.txt"
    Name "MyTempFile.txt" As "MyFile.txt"
    The text file that resulted was,

    %
    O0000
    M00
    ( Menu File: W.120 )
    ( 12-05-2006 19:00:41 )
    N 1 G01G90G49G40G80G17
    N 2 G91G28Z0
    N 3 G91G28X0Y0A0.
    N 4 T 12M06
    N 5 G92X 119450 Y 151150 Z 229765 A- 80
    N 6 G01G90Y 0 F 2000000
    N 7 S 2700 M03
    N 8 M08
    N 9 G90A0.F 2000000
    %


    I have attached the project files
    Attached Files Attached Files

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: Text file stuff

    Hey Thank for the help

    Reston

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