Jump to content

File writing

- - - - -

  • Please log in to reply
3 replies to this topic

#1
CrazyVB

CrazyVB

    Newbie

  • Members
  • PipPip
  • 20 posts
Ok, so i am making this program, that can save a text file without opening any windows.
Imeda a text file called "cache", now in the program i want for it to be able to read it and write on it. Like this:
-program opens and loads the text (the text is in lines)
-copies line by line to a combobox
-when the program is asked, it saves a new line with text in to the "cache" file
Please help

CrazyVB

-----------------------------
Evrything is possible...

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 405 posts
Every language has it API for file handling support so you can choose the one you like.

For e.g. if you use C, there is an API called fopen to open a file i.e.


FILE *fp = fopen("cache", "rw");


if(fp != NULL)

{

    // read

    // write

}


fclose(fp);

"

The functions to use for reading and writing could be fgets or fputs.

Ah, i see you mentioning combo box, so probably you are using any of C# or java each of which has its own API.
FileInputStreamReader / Writer stuff. But the idea is same as above.

#3
CrazyVB

CrazyVB

    Newbie

  • Members
  • PipPip
  • 20 posts
Ok now there is another problem; when I click on the button to open a form it pauses debuging and shows this:

Quote

An error occurred creating the form. See Exception.InnerException for details. The error is: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

The code of the form(the one I want to open) is:

Public Class bcolor


    Private Sub Style_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Images.Image = Bigfoot.background.BackgroundImage

        curentcolor.text = My.Settings.color

    End Sub




    Private Sub video_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles video.CheckedChanged

        Static lol

        lol = 0 Or 1 Or 2 Or 3 Or 4 Or 5 Or 6 Or 7 Or 8

        If video.Checked = False Then

            Timer1.Stop()

            Colors.Enabled = True

            test.Enabled = True

            okstyle.Enabled = True

            If Colors.SelectedItem = "Blue" Then

                lol = 8

                Images.Image = ImageList1.Images(lol)

            End If

            If Colors.SelectedItem = "Green" Then

                lol = 1

                Images.Image = ImageList1.Images(lol)

            End If

            If Colors.SelectedItem = "Orange" Then

                lol = 2

                Images.Image = ImageList1.Images(lol)

            End If

            If Colors.SelectedItem = "Pink" Then

                lol = 3

                Images.Image = ImageList1.Images(lol)

            End If

            If Colors.SelectedItem = "Purple" Then

                lol = 4

                Images.Image = ImageList1.Images(lol)

            End If

            If Colors.SelectedItem = "Red" Then

                lol = 5

                Images.Image = ImageList1.Images(lol)

            End If

            If Colors.SelectedItem = "White" Then

                lol = 6

                Images.Image = ImageList1.Images(lol)

            End If

            If Colors.SelectedItem = "Yellow" Then

                lol = 7

                Images.Image = ImageList1.Images(lol)

            End If


        Else

            Timer1.Start()

            Colors.Enabled = False

            test.Enabled = False

            okstyle.Enabled = False

        End If


        omg.Text = ImageList1.Images.Keys(lol)

        If omg.Text = "blue.png" Then

            omg.Text = "Blue"

        End If

        If omg.Text = "green.png" Then

            omg.Text = "Green"

        End If

        If omg.Text = "orange.png" Then

            omg.Text = "Orange"

        End If

        If omg.Text = "pink.png" Then

            omg.Text = "Pink"

        End If

        If omg.Text = "purple.png" Then

            omg.Text = "Purple"

        End If

        If omg.Text = "red.png" Then

            omg.Text = "Red"

        End If

        If omg.Text = "white.png" Then

            omg.Text = "White"

        End If

        If omg.Text = "yellow.png" Then

            omg.Text = "Yellow"

        End If

    End Sub


    Private Sub Colors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Colors.SelectedIndexChanged

        Static lol

        lol = 0 Or 1 Or 2 Or 3 Or 4 Or 5 Or 6 Or 7 Or 8


        If Colors.SelectedItem = "Blue" Then

            lol = 8

            Images.Image = ImageList1.Images(lol)

        End If

        If Colors.SelectedItem = "Green" Then

            lol = 1

            Images.Image = ImageList1.Images(lol)

        End If

        If Colors.SelectedItem = "Orange" Then

            lol = 2

            Images.Image = ImageList1.Images(lol)

        End If

        If Colors.SelectedItem = "Pink" Then

            lol = 3

            Images.Image = ImageList1.Images(lol)

        End If

        If Colors.SelectedItem = "Purple" Then

            lol = 4

            Images.Image = ImageList1.Images(lol)

        End If

        If Colors.SelectedItem = "Red" Then

            lol = 5

            Images.Image = ImageList1.Images(lol)

        End If

        If Colors.SelectedItem = "White" Then

            lol = 6

            Images.Image = ImageList1.Images(lol)

        End If

        If Colors.SelectedItem = "Yellow" Then

            lol = 7

            Images.Image = ImageList1.Images(lol)

        End If


    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelstyle.Click

        Me.Close()

    End Sub


    Private Sub test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles test.Click

        Dim BG As Bigfoot

        BG.background.Image = Images.Image

        BG.back.BackgroundImage = Images.Image

        BG.forward.BackgroundImage = Images.Image

        BG.refreshpage.BackgroundImage = Images.Image

        BG.fav.BackgroundImage = Images.Image

        BG.home.BackgroundImage = Images.Image

        BG.go.BackgroundImage = Images.Image

        BG.MenuStrip1.BackgroundImage = Images.Image

    End Sub


    Private Sub okstyle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okstyle.Click

        Dim BG As Bigfoot

        BG.background.Image = Images.Image

        BG.back.BackgroundImage = Images.Image

        BG.forward.BackgroundImage = Images.Image

        BG.refreshpage.BackgroundImage = Images.Image

        BG.fav.BackgroundImage = Images.Image

        BG.home.BackgroundImage = Images.Image

        BG.go.BackgroundImage = Images.Image

        BG.MenuStrip1.BackgroundImage = Images.Image

        My.Settings.color = omg.Text

        Me.Close()

    End Sub

End Class

Thx for the code, but it doesn't work evrything is underlined red

#4
btu

btu

    Newbie

  • Members
  • Pip
  • 1 posts
If you are using VB .NET, I might suggest IO.StreamReader class.
This reads the text file line by line until the end of file marker. Each iteration through the loop, it will store the line of text in a variable, and insert it into the combo box.


        Dim inFile As IO.StreamReader

        Dim vstring as string

        Dim path as string = InputBox("enter path of file") 'Enter file path manually

        If Not path = String.Empty Then

        If IO.File.Exists(path) Then

            inFile = IO.File.OpenText(path)

            Do Until inFile.Peek = -1 'Loop until EOF

                string = inFile.ReadLine

                combobox.items.add(vstring)

            End If

        Loop

            inFile.Close()

        End If


Edited by btu, 15 May 2011 - 06:50 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users