streamreader add to array

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1097 Views - Last Post: 11 June 2019 - 07:50 PM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

streamreader add to array

Posted 09 June 2019 - 11:45 AM

Hi, I have these codes, I wonder how I can add textfile to an array by reading the text file with streamreader

StreamReader reader = new StreamReader("texter.txt", Encoding.Default, false);
string[] parts = s.Split('\t');

Is This A Good Question/Topic? 0
  • +

Replies To: streamreader add to array

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: streamreader add to array

Posted 09 June 2019 - 11:59 AM

REad the docs. Specifically 'readline' and use that with a loop.

https://docs.microso...etframework-4.8
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: streamreader add to array

Posted 09 June 2019 - 12:11 PM

Yet again, this is another case where if you took time to learn the language in a more structured manner, you would have been introduced to this in a better way. If you persist on doing this shotgun approach, then it behooves you to teach yourself to read the documentation.
Was This Post Helpful? 0
  • +
  • -

#4 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader add to array

Posted 09 June 2019 - 12:31 PM

"could not find the file"

StreamReader file = new System.IO.StreamReader(@"C:\Users\razer\Desktop\test.txt");


Text file in properties says "C:\Users\razer\Desktop" the file name is "test.txt"

Why does the streamreader not find the file?

Any suggestions on what I need to write in the streamreader?

skydiver, I was using this thing 1 year ago but I forgot by now. Before it was fully functional. The name thing is causing problem
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: streamreader add to array

Posted 09 June 2019 - 12:44 PM

Copy/paste the error messages.
Was This Post Helpful? 0
  • +
  • -

#6 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader add to array

Posted 09 June 2019 - 12:47 PM

Okay I partly found out the problem, I had saved the file name as "test.txt" I only needed to save as "test" however, the file is being read but this is the wrong message I get, when I run the console application this is being shown

"System.IO.Streamreader"

this is my code:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
         

StreamReader file = new StreamReader(@"C:\Users\razer\Desktop\test.txt");

            Console.WriteLine(file);
                            Console.ReadLine();
                        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader add to array

Posted 09 June 2019 - 12:52 PM

Ehm.. Problem solved.. I had written wrong writeline.

This is correct:

Console.WriteLine(file.ReadLine());

Was This Post Helpful? 1
  • +
  • -

#8 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader add to array

Posted 09 June 2019 - 01:33 PM

Hi,

I have this following code:

using System;
using System.IO;
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;

namespace WindowsFormsApp2
{
    public class Biblotekarie
    {
        

            

        public void inläsning()
        {
            
            

        }

    }
    
    public partial class Form1 : Form
    {

        List<string> Spara = new List<string>();

        

        string[] animals2 = new string[] { "Författare Pippi långstrump, Roman, Årstyp"
            , "Författare Pippi långstrump, Roman, Årstyp"
            , "Tue", "Wed", "Thu", "Fri", "sat" };

        public static Random rng;
        

        public Form1()
        {
          
            InitializeComponent();
            StreamReader file = new StreamReader(@"C:\Users\razer\Desktop\test.txt", Encoding.Default, false);
            string item = "";
            while ((item = file.ReadLine()) != null)
            {
                Spara.Add(item);
            }
            //Spara.Add(animals2);
            rng = new Random();

            foreach (string a in Spara)
            {
                string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            
                int index = rng.Next();

            foreach (string item in Spara)
                
            {
                
                textBox1.Text = Convert.ToString(item);
                
            }
            Biblotekarie bib = new Biblotekarie();
           
            
        }

        private void SökRuta(object sender, EventArgs e)
        {
            
        }
    }
}


This makes the reader to read only the last code in the filetext. How can I make it read one whole line at a time?
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: streamreader add to array

Posted 09 June 2019 - 01:45 PM

Come on now.. read your for loop. What happens? You are over writing things each iteration. Well.. yeah.. no kidding the last thing shown would be the last thing in the list.
67
	            foreach (string item in Spara)
68
	                 
69
	            {
70
	                 
71
	                textBox1.Text = Convert.ToString(item);
72
	                 
73
	            }

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: streamreader add to array

Posted 09 June 2019 - 10:53 PM

Also, on lines 67-73, item is already a string. There is no need to convert it to a string.
Was This Post Helpful? 0
  • +
  • -

#11 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader add to array

Posted 10 June 2019 - 01:53 AM

Hi,

I cant get my program to read different lines of the textfile and write a whole line each time i run the program.

The rng index, what am I supposed to do in it to get my whole line written out?

using System;
using System.IO;
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;

namespace WindowsFormsApp2
{
    public class Biblotekarie
    {
        

            

        public void inläsning()
        {
            
            

        }

    }
    
    public partial class Form1 : Form
    {

        List<string> Spara = new List<string>();

        

        string[] animals2 = new string[] { "Författare Pippi långstrump, Roman, Årstyp"
            , "Författare Pippi långstrump, Roman, Årstyp"
            , "Tue", "Wed", "Thu", "Fri", "sat" };

        public static Random rng;
        

        public Form1()
        {
          
            InitializeComponent();
            StreamReader file = new StreamReader(@"C:\Users\razer\Desktop\test.txt", Encoding.Default, false);
            string item = "";
            while ((item = file.ReadLine()) != null)
            {
                
                Spara.Add(item);
            }
            //Spara.Add(animals2);
            rng = new Random();

            foreach (string a in Spara)
            {
                string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            
                int index = rng.Next(1,4);

            foreach (string item in Spara)
                
            {
                
                textBox1.Text = Convert.ToString(item[index]);
                
            }
            
           
            
        }

        private void SökRuta(object sender, EventArgs e)
        {
            
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,097
  • Joined: 12-December 12

Re: streamreader add to array

Posted 10 June 2019 - 02:41 AM

            foreach (string a in Spara)
            {
                string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
            }

This won't achieve anything because vektor is a local variable that will just vanish when the method ends.

You appear to have skipped the point raised in post #9.

textBox1.Text = Convert.ToString(item); If you want all the lines printed then you could use a multiline textbox and add new lines to it for each string.
Was This Post Helpful? 0
  • +
  • -

#13 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader add to array

Posted 10 June 2019 - 02:45 AM

when I do this it only shown numbers

for (int i = 0; i < Spara.Count; i ++)
            {
                textBox1.Text = Convert.ToString(i);
            }

Was This Post Helpful? 0
  • +
  • -

#14 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,097
  • Joined: 12-December 12

Re: streamreader add to array

Posted 10 June 2019 - 03:02 AM

Yes. i is an integer, a number. You are then converting this number to a string to set as the text of the textbox.
Was This Post Helpful? 1
  • +
  • -

#15 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader add to array

Posted 10 June 2019 - 03:07 AM

Yes it works now when I wrote like this:

or (int i = 0; i < Spara.Count; i++)
                
            {
                
                textBox1.Text = Convert.ToString(Spara[index]);
                
            }


btw you are right... silly of me

This post has been edited by Realme: 10 June 2019 - 03:10 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2