1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to save input to a text file?

Discussion in 'C#' started by JJnacy, May 19, 2009.

  1. #1
    example:
    dates from May 1st to 31st total 31 days, so there are 31 notes need to be submitted.
    With a form to submit my notes and I want to save it to mynote.txt file not excel or SQL DB.

    today=date
    todaynote=request("todaynote")

    How to save them into mynote.txt page?

    Thanks
     
    JJnacy, May 19, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I'm assuming that you are talking about Classic ASP, here is some code to get you started.

    
    Dim objFso, objFile
    
    Set objFso = Server.CreateObject("Scripting.FileSystemObject")
    Set objFile=objFso.OpenTextFile(Server.MapPath("mynote.txt"),8,true)
    
    objFile.WriteLine(today)
    objFile.WriteLine(todaynote)
    objFile.Close
    
    Set objFile = Nothing
    Set objFso = Nothing
    
    Code (markup):
    The '8' in the following line is the mode:
    Set f=fs:confused:penTextFile(Server.MapPath("mynote.txt"),8,true)

    mode 1 is ForReading
    mode 2 is ForWriting
    and mode 8 is ForAppending

    The 'true' paramater is saying to create the file if it doesn't already exist.

    Other methods of the textstream object (objFile), depending on the mode are:
    objFile.Write(string)
    objFile.Read(length)
    objFile.ReadAll
    objFile.ReadLine
     
    camjohnson95, May 19, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    But I would recommend using a database, you could still output the comments as a textfile but you could easily refine the dates and delete and edit the comments much more easily
     
    camjohnson95, May 19, 2009 IP
  4. JJnacy

    JJnacy Peon

    Messages:
    448
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you Camjohnson95,

    Could you guide me how to add the 2nd day's note to the mynote.txt without deleting the 1st day's note.


    Thank You / Best Regards,
     
    JJnacy, May 19, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Yes just use the same method, as it is opening the file in ForAppending mode (8) it will just add the new lines to the end of the file, ForWriting will overwrite the existing entries.
     
    camjohnson95, May 19, 2009 IP
  6. JJnacy

    JJnacy Peon

    Messages:
    448
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you Camjohnson95,

    Excuse me, I was thinking how to edit the mynote.txt

    What if I use split any array to read mynote.txt (by mode 1 / ForReading )
    then pick up the date which I want to edit and submit all new mynote string again (by mode 2 / ForWriting)

    Now my questions are:
    1. What is the max size for a .txt file?
    2. Will it slow down server by calling or saving the large size of mynote.txt?


    Thank You / Best Regards,
     
    JJnacy, May 20, 2009 IP
  7. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Yes it is possible to edit the file and insert or delete records by reading it all, making the changes, then rewriting it all back. There isn't really a max size, but if the file get's too large it will start to take longer to perform some tasks (especially editing and deleting records). This is a rough estimation but I reckon upto about 5000 records would be about the most that you would want to have if you are using this method, but i may be wrong. I would recommend that if you really want to use this method then use seperate textfiles for each date. E.g 01/05/09 would be stored in May2009.txt.
     
    camjohnson95, May 20, 2009 IP
  8. JJnacy

    JJnacy Peon

    Messages:
    448
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks for the good suggestion.

    Best Regards,
     
    JJnacy, May 20, 2009 IP