Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2015-05-10 09:40:06, last edited by 1448 (2015-05-10 12:18:51)

1448
Formerly alkazam1448
From: Numberland
Joined: 2015-04-12
Posts: 683
Website

[Solved] [Code] Streams and text files

Read DarkDragon's replies

Offline

#2 2015-05-10 09:45:14

madiik
Member
From: floor above Yuuta
Joined: 2015-02-26
Posts: 514

Re: [Solved] [Code] Streams and text files

Make a list to an array. Add context by WriteAllText or WriteAllLines.

OPEN stream with,

System.IO.StreamWrite fs = new System.IO.StreamWriter()

Don't forget to dispose (1) and close (2) after you are done.


shh i have returned

Offline

#3 2015-05-10 09:51:26, last edited by 1448 (2015-05-10 09:53:16)

1448
Formerly alkazam1448
From: Numberland
Joined: 2015-04-12
Posts: 683
Website

Re: [Solved] [Code] Streams and text files

madiik wrote:

Make a list to an array. Add context by WriteAllText or WriteAllLines.

OPEN stream with,

System.IO.StreamWrite fs = new System.IO.StreamWriter()

Don't forget to dispose (1) and close (2) after you are done.

What do you mean by "open" steam?

Extra:
How do you convert a list into an array?

How do you do File.WriteAllLines() to an array?

Offline

#4 2015-05-10 10:49:58

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: [Solved] [Code] Streams and text files

alkazam1448 wrote:
Hidden text

What do you mean by "open" steam?

Extra:
How do you convert a list into an array?

How do you do File.WriteAllLines() to an array?

Opening a stream means that you can send data through (on phone, but I think by doing: streamName.Write(File))

I would appreciate if you actually tried looking yourself, since list to array conversion is literally:
Array = List.toArray()

Again thinking for yourself would be nice.
If you would like the textfile to be readable you should first print the size of the array, then loop through each element of the array, print each element and done.
Reading would be first reading the size and then looping the size amount of times to get all elements again without reading elements that are not part of the array.
If the textfile isn't meant to be read you can just do:
File.Write(Array)
You might need to add .toString() to the array name.

P.S. @madiik I don't see why you should covert the List to an Array.

Offline

#5 2015-05-10 11:10:42

1448
Formerly alkazam1448
From: Numberland
Joined: 2015-04-12
Posts: 683
Website

Re: [Solved] [Code] Streams and text files

ListBox, not list

Offline

#6 2015-05-10 11:13:39

DarkDragon4900
Member
Joined: 2015-03-17
Posts: 251

Re: [Solved] [Code] Streams and text files

You don't need to close and dispose.
Closing is enough.
Close()'s code is

public override void Close()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}

in the streamwriter,
and

public override void Close()
{
    this.Dispose(true);
}

in the streamreader.
And you don't need to call any of them if you wrap it in a 'using'.

You can save items by writing lines and reading them line by line when loading.
You can create a method of yours that makes it easier.

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("email", "[email protected]");
d.Add("pass", "*****");
d.Add("wid", "PW...");

Save(Application.StartupPath + @"\data.txt", d);
Dictionary<string, string> loadedDictionary = new Dictionary<string, string>();
Load(Application.StartupPath + @"\data.txt", out loadedDictionary);
tbEmail.Text = loadedDictionary["email"];
tbPass.Text = loadedDictionary["pass"];
tbWid.Text = loadedDictionary["wid"];

public void Save(string path, Dictionary<string, string> d)
{
    using (System.IO.StreamWriter w = new System.IO.StreamWriter(path))
    {
        foreach (KeyValuePair<string, string> kv in d)
        {
            w.WriteLine(kv.Key + ":" + kv.Value);
        }
    }
}

public void Load(string path, out Dictionary<string, string> d)
{
    Dictionary<string, string> dd = new Dictionary<string, string>();
    using (System.IO.StreamReader r = new System.IO.StreamReader(path))
    {
        string line = "";
        while ((line = r.ReadLine()) != null)
        {
            dd.Add(line.Split(':')[0], line.Split(':')[1]);
        }
    }
    d = dd;
}

Offline

#7 2015-05-10 11:33:23

1448
Formerly alkazam1448
From: Numberland
Joined: 2015-04-12
Posts: 683
Website

Re: [Solved] [Code] Streams and text files

DarkDragon4900 wrote:

You don't need to close and dispose.
Closing is enough.
Close()'s code is

public override void Close()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}

in the streamwriter,
and

public override void Close()
{
    this.Dispose(true);
}

in the streamreader.
And you don't need to call any of them if you wrap it in a 'using'.

You can save items by writing lines and reading them line by line when loading.
You can create a method of yours that makes it easier.


Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("email", "[email protected]");
d.Add("pass", "*****");
d.Add("wid", "PW...");

Save(Application.StartupPath + @"\data.txt", d);
Dictionary<string, string> loadedDictionary = new Dictionary<string, string>();
Load(Application.StartupPath + @"\data.txt", out loadedDictionary);
tbEmail.Text = loadedDictionary["email"];
tbPass.Text = loadedDictionary["pass"];
tbWid.Text = loadedDictionary["wid"];

public void Save(string path, Dictionary<string, string> d)
{
    using (System.IO.StreamWriter w = new System.IO.StreamWriter(path))
    {
        foreach (KeyValuePair<string, string> kv in d)
        {
            w.WriteLine(kv.Key + ":" + kv.Value);
        }
    }
}

public void Load(string path, out Dictionary<string, string> d)
{
    Dictionary<string, string> dd = new Dictionary<string, string>();
    using (System.IO.StreamReader r = new System.IO.StreamReader(path))
    {
        string line = "";
        while ((line = r.ReadLine()) != null)
        {
            dd.Add(line.Split(':')[0], line.Split(':')[1]);
        }
    }
    d = dd;
}

Thanks, DarkDragon! Also, any ideas on how to save items in a ListBox and load them?

Offline

#8 2015-05-10 11:41:55, last edited by DarkDragon4900 (2015-05-10 11:44:24)

DarkDragon4900
Member
Joined: 2015-03-17
Posts: 251

Re: [Solved] [Code] Streams and text files

alkazam1448 wrote:
"quote"

Thanks, DarkDragon! Also, any ideas on how to save items in a ListBox and load them?

You can modify the methods to suit your needs.

public void Save(string path, ListBox l)
{
    using (System.IO.StreamWriter w = new System.IO.StreamWriter(path))
    {
        foreach (var item in l.Items)
        {
            w.WriteLine(item.ToString());
        }
    }
}

public void Load(string path, out ListBox l)
{
    ListBox ll = new ListBox();
    using (System.IO.StreamReader r = new System.IO.StreamReader(path))
    {
        string line = "";
        while ((line = r.ReadLine()) != null)
        {
            ll.Items.Add(line);
        }
    }
    l = ll;
}

And it seems you're having trouble hiding text.
Use

[spoiler="Spoiler Text Here"]Spoiler Content Here.[/spoiler]

To produce

"Spoiler Text Here"

Offline

#9 2015-05-10 11:44:36

1448
Formerly alkazam1448
From: Numberland
Joined: 2015-04-12
Posts: 683
Website

Re: [Solved] [Code] Streams and text files

Thanks man you are the best. Can't imagine what would've happened to my broken ugly code without your intervention. Thank you.

Offline

#10 2015-05-10 11:45:28

DarkDragon4900
Member
Joined: 2015-03-17
Posts: 251

Re: [Solved] [Code] Streams and text files

Np :p

Offline

#11 2015-05-10 19:36:11

madiik
Member
From: floor above Yuuta
Joined: 2015-02-26
Posts: 514

Re: [Solved] [Code] Streams and text files

The fact that Visual Studio even warns you about .Close(), it says you should dispose first, then close.

It's safer.


shh i have returned

Offline

#12 2015-05-11 11:08:55, last edited by DarkDragon4900 (2015-05-11 11:21:24)

DarkDragon4900
Member
Joined: 2015-03-17
Posts: 251

Re: [Solved] [Code] Streams and text files

It isn't.
Since close obviously executes the Dispose method.

Dispose():
"Releases all resources used by the System.IO.Text(writer/reader) object."

Close():
"Closes the System.IO.Stream(Writer/Reader) object and the underlying stream, and releases any system resources associated with the reader."
Besides, no warnings show up when you use the Close() method.

[Edit] Read about it in stackoverflow.

Offline

#13 2015-05-11 11:46:39

Hexagon
Member
Joined: 2015-04-22
Posts: 1,213

Re: [Solved] [Code] Streams and text files

madiik wrote:

The fact that Visual Studio even warns you about .Close(), it says you should dispose first, then close.


Calling Dispose() will do close() then Dispose() if close is already called.

If you Dispose() it will close() then if you try to close() you'll get an exception.

Offline

Wooted by:
Hexagon1431341199503538

Board footer

Powered by FluxBB

[ Started around 1732806420.0779 - Generated in 0.130 seconds, 13 queries executed - Memory usage: 1.59 MiB (Peak: 1.79 MiB) ]