Results 1 to 20 of 20

Thread: Very Important Program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    16

    Very Important Program

    I have a program that uses arrays to convert the state names into abbreviations (like you like in Kansas and get out KS in a seperate box) that works fine but for my final project DUE TOMORROW i have to edit it and make the arrays run from a text file instead and i have NO CLUE how to do this. Please help

  2. #2
    Addicted Member
    Join Date
    Aug 2005
    Location
    Chesterfield, UK
    Posts
    162

    Re: Very Important Program

    Do you just need to read a text file? if so try this

    VB Code:
    1. Dim InputState as String
    2. FileOpen(1,"states.txt",OpenMode.Input)
    3. While not EOF(1)
    4.   InputState = LineInput(1)
    5.   'store it into your array
    6. End While
    7.  
    8. FileClose(1)

    To write the array back out just open another file in Output mode and use WriteLine.

  3. #3
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    VB Code:
    1. Dim fileReader As System.IO.StreamReader
    2. fileReader = _
    3. My.Computer.FileSystem.OpenTextFileReader("C:\\testfile.txt")
    4. Dim stringReader As String
    5. While fileReader.Peek() <> -1
    6.     stringReader = fileReader.ReadLine()
    7.     MessagBox.Show("The line of the file is " & stringReader)
    8. Loop
    Last edited by Jumpercables; Apr 20th, 2006 at 11:02 AM.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    16

    Re: Very Important Program

    i have tried both of those codes but neither of them work right (im very new to this)

  5. #5
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    How do the States appear in the file ?

    each state on one line?

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    16

    Re: Very Important Program

    yeah each on a seperate line

  7. #7
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    Whoops! I typed the loop wrong - change it to the following

    VB Code:
    1. Dim fileReader As System.IO.StreamReader
    2.         fileReader = _
    3.         My.Computer.FileSystem.OpenTextFileReader("C:\\testfile.txt")
    4.         Dim stringReader As String
    5.         Do While fileReader.Peek() <> -1
    6.             stringReader = fileReader.ReadLine()
    7.         Loop
    8.  
    9.         fileReader.Close()

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    16

    Re: Very Important Program

    when i do that it says my is not declared

  9. #9
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    Hmm odd you using VB.NET?
    Last edited by Jumpercables; Apr 20th, 2006 at 11:21 AM.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    16

    Re: Very Important Program

    microsoft visual basic .NET

  11. #11
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    Well then lets try doing the following.

    VB Code:
    1. Dim fileReader As New System.IO.StreamReader("C:\\testfile.txt")
    2.  
    3.         Dim stringReader As String = Nothing
    4.         ' Initalize array for the 50 states
    5.         Dim statesArray(50) As String
    6.         Dim lineCount As Integer = 0
    7.  
    8.         ' Read file until end of file.
    9.         Do While fileReader.Peek() <> -1
    10.             stringReader = fileReader.ReadLine()
    11.             ' Load line state into the array that holds full state names
    12.             statesArray.SetValue(stringReader, lineCount)
    13.             lineCount += 1
    14.         Loop
    15.  
    16.         ' Dont forget to close the stream reader
    17.         fileReader.Close()
    18.         fileReader = Nothing

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    16

    Talking Re: Very Important Program

    thanks alot!! worked perfectly!

  13. #13
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    Sorry about the My error thats only in VS2005 and not 2003

    No Problem

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    16

    Re: Very Important Program

    erm i have a new problem now, i have to have an option to add to one of the text files. any hints on how to do that?

  15. #15
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    Adding information into the text file you read or create a new text file?

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  16. #16
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    Using the StreamWriter Class you can manipulate files.

    VB Code:
    1. Dim filepath As String = "C:\MyText.txt"
    2. ' If the file doen't exists create it.
    3. If Not IO.File.Exists(filepath) Then IO.File.Create(filepath)
    4.  
    5. ' Initalize Streamwriter
    6. Dim fileWriter As New System.IO.StreamWriter(filepath)
    7. fileWriter.WriteLine("This writes a line to the file")
    8.  
    9. ' Clear buffer and close stream
    10. fileWriter.Flush()
    11. fileWriter.Close()

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  17. #17
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    I just noticed that clears all the contents in the file and doesn't append the information.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  18. #18
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Very Important Program

    Here this will append to a file

    VB Code:
    1. ' Initalize Streamwriter
    2. Using fileWriter As IO.StreamWriter = IO.File.AppendText(filepath)
    3.      fileWriter.WriteLine("This appends")
    4.      fileWriter.WriteLine("append again on new line")
    5.      fileWriter.Flush()
    6.      fileWriter.Close()
    7. End Using

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    16

    Re: Very Important Program

    that doesn't seem to work just gets an error (sorry for long reply i was going home from school) the Using fileWriter As IO.StreamWriter = IO.File.AppendText(filepath)
    line doesnt seem to be valid the using and filewriter and as is underlined in blue saying using is not declared and such
    Last edited by kojicolnair; Apr 20th, 2006 at 01:28 PM.

  20. #20
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Very Important Program

    Jumpercables, good job doing his homework for him. Next time offer points, otherwise you don't learn much AND it's considered academic dishonesty, which can get him expelled if his teacher find this thread.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

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