4 Replies - 601 Views - Last Post: 31 October 2015 - 09:11 AM Rate Topic: -----

#1 BoolAId   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 15

How do I write a list to file?

Posted 31 October 2015 - 08:14 AM

My code thus far...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Random_Number_File_Writer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void generate_Click(object sender, EventArgs e)
        {
            int numberOfElems = int.Parse(randomNumberAmountTextBox.Text);
            Random r = new Random();
            List<int> myNumbers = new List<int>();
            for (int i = 0; i < numberOfElems; ++i)
            {
                int number = r.Next(0, 100); // will bring a random number from 0 to 99
                myNumbers.Add(number);

                new StreamWriter("d:\\file.txt", true);

            }
        }

        }
    }
}



The above doesn't even work (specifically "new StreamWriter("d:\\file.txt", true);"). Basically the program asks for a amount of numbers to be generated, program generates them, and then I want it to write to a .txt file (preferably with a prompt for location).

So, how do I write a list to file?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I write a list to file?

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: How do I write a list to file?

Posted 31 October 2015 - 08:26 AM

Quote

The above doesn't even work

Of course it doesn't work, you've only written a single, incorrect, line of code.

See the example at the docs.

You need to demonstrate more of an effort, there is tonnes of information about writing to a file.

Quote

(preferably with a prompt for location)

There is a wealth of information available for this as well, producing a file dialog.

This post has been edited by andrewsw: 31 October 2015 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#3 BoolAId   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 15

Re: How do I write a list to file?

Posted 31 October 2015 - 08:49 AM

There is more than a single line of code if you had bothered to read the whole thing. I have the input, and random generation down. I just don't know how to print a list into a file.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: How do I write a list to file?

Posted 31 October 2015 - 08:57 AM

There is only a single line that makes the vaguest attempt to write to a file. You cannot write this single, incorrect, line and expect us to walk you through the process, especially when there is so much information available to you. Read the link I provided and search "c# write to file" or similar.

It is required that you demonstrate effort, a single line doesn't count.
Was This Post Helpful? 0
  • +
  • -

#5 BoolAId   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 15

Re: How do I write a list to file?

Posted 31 October 2015 - 09:11 AM

Ok so I followed this, and now I actually have the program successfully writing to a .txt file.

        private void generate_Click(object sender, EventArgs e)
        {
            int numberOfElems = int.Parse(randomNumberAmountTextBox.Text);
            Random r = new Random();
            List<int> myNumbers = new List<int>();
            for (int i = 0; i < numberOfElems; ++i)
            {
                int number = r.Next(0, 100); // will bring a random number from 0 to 99
                myNumbers.Add(number);

                string[] lines = { "First line", "Second line", "Third line" };

                using (StreamWriter outputFile = new StreamWriter(@"d:\test.txt"))
                {
                    foreach (string line in lines)
                        outputFile.WriteLine(line);
                }
        }



How would I get the program do I get the random numbers to replace "string[] lines = { "First line", "Second line", "Third line" };" though?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1