streamreader and writer into same textfile

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 697 Views - Last Post: 15 June 2019 - 10:55 AM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

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

streamreader and writer into same textfile

Posted 11 June 2019 - 11:52 AM

hi,

I have made so that I read form a text file and add these into a arraylist. later I make so that I write in another file and want to add this in the same list. The problem is that it does not work because the command controller cant read and write at the same time.

However, I have made two different textfiles to begin with as example to see where it is going but I cant even get the new textfiles attached to the listarray. Here is my 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 partial class Form1 : Form
    {
        
        List<string[]> VLista = new List<string[]>();
        List<string> Spara = new List<string>();
        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);
               
            }
            

            rng = new Random();

            foreach (string a in Spara)
            {
                string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
                
                    VLista.Add(vektor);
                
            }
        }
       
        private void Button1_Click(object sender, EventArgs e)
        {
            int index = rng.Next(0, 12);
            
                
                textBox1.Text = Convert.ToString(VLista[index][0] + VLista[index][1] + VLista[index][2] + VLista[index][3]);
            
        }
        
        private void SökRuta(object sender, EventArgs e)
        {
            
        }

        
        private void Button2_Click(object sender, EventArgs e)
        {
            string[] lines = { richTextBox1.Text };
            string docPath =
              Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            // Write the string array to a new file named "WriteLines.txt".
            using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, @"C:\Users\razer\Desktop\inlägg.txt")))
            {

                
                foreach (string line in lines)
                    outputFile.WriteLine(richTextBox1.Text);

            }

            // Create a string array with the lines of text

            // Set a variable to the Documents path.
        
        }
    }
}


I have tried to make an "add" inside spara as following Spara.Add(rich.....ToString());

error code in arraylist

Is This A Good Question/Topic? 0
  • +

Replies To: streamreader and writer into same textfile

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 11:58 AM

Please explain the problem you are having in depth, and maybe ask a question.

Quote

The problem is that it does not work because the command controller cant read and write at the same time.

What?
Was This Post Helpful? 0
  • +
  • -

#3 Realme   User is offline

  • D.I.C Head

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 12:18 PM

what I want to do is to add new text into StreamReader file = new StreamReader(@"C:\Users\razer\Desktop\test.txt" , Encoding.Default, false);

how can I do that?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 12:20 PM

So you do not write new text to a file with a Stream Reader. You read it from a source. If you want to write to a file you use Stream Writer.
Was This Post Helpful? 0
  • +
  • -

#5 Realme   User is offline

  • D.I.C Head

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 12:25 PM

I know that modi, look at my codes I pasted in the beginning and you will see I am trying.

When I change the text from writer to test.txt from inlägg.txt it gives me error message "the process cant access file"...…" because is being used
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 12:33 PM

Man.. what are you doing!?

Why are you combining one path with another here? Have you even debugged your code, stepped through it, or considered what you are just shoveling around?

66            string docPath =
67              Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
68 
69            // Write the string array to a new file named "WriteLines.txt".
70            using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, @"C:\Users\razer\Desktop\inlägg.txt")))



Why is this loop going by lines in some collection, but writing the entire contents of some random richtextbox ?!
74                foreach (string line in lines)
75                    outputFile.WriteLine(richTextBox1.Text);



Stop and think of what you are doing. clean it up.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 12:34 PM

This looks very suspicious:
string docPath =
              Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, @"C:\Users\razer\Desktop\inlägg.txt")))


In particular this part: Path.Combine(docPath, @"C:\Users\razer\Desktop\inlägg.txt")
If you already have a fullpath in the form of "C:\Users\razer\Desktop\inlägg.txt", why are you trying to prepend the current user's "My Documents" folder to it? I'm willing to bet that the exception/error message presented to you gives you a path that is invalid because of this poor path building that you are doing.

Looks like Modi was typing faster than I was. He also addressed the inconsistent logic for writing contents out to the file.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 12:35 PM

@skydiver - actually the function is saving his bacon there.

Quote

Returns
String
The combined paths. If one of the specified paths is a zero-length string, this method returns the other path. If path2 contains an absolute path, this method returns path2.

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

#9 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 12:36 PM

But I'll also point out that this doesn't make sense at all:
string[] lines = { richTextBox1.Text };



This won't magically breakup the contents of the RichTextBox into an array of lines. What the above will do is put all the contents of the RichTextBox into a single string array element of lines.
Was This Post Helpful? 0
  • +
  • -

#10 Realme   User is offline

  • D.I.C Head

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 12:54 PM

Here is my edited new 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 partial class Form1 : Form
    {
        
        List<string[]> VLista = new List<string[]>();
        List<string> Spara = new List<string>();
        public static Random rng;
        

        public Form1()
        {
            
            
            InitializeComponent();
            rng = new Random();
            string[] lines = { richTextBox1.Text };

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



            foreach (string a in Spara)
            {
                string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
                
                    VLista.Add(vektor);
                
            }
            VLista.Add(lines);
        }
       
        private void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < VLista.Count; i++)
            {
                int index = rng.Next(0, i);


                textBox1.Text = Convert.ToString(VLista[index][0] + VLista[index][1] + VLista[index][2] + VLista[index][3]);
            }
            
        }
        
        private void SökRuta(object sender, EventArgs e)
        {
            
        }

        
        private void Button2_Click(object sender, EventArgs e)
        {
          
            using (StreamWriter outputFile = new StreamWriter( @"C:\Users\razer\Desktop\inlägg.txt"))
            {
                    outputFile.WriteLine(richTextBox1.Text);
                
            }
            
            
        }
    }
}



Why does it not show my new code in "inlägg.txt" in vlista?
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 01:09 PM

Pretty sure that works.

As usual copy/paste the ENTIRE error messages (if there area any), and point out which line they are happening on.
Was This Post Helpful? 0
  • +
  • -

#12 Realme   User is offline

  • D.I.C Head

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 01:17 PM

Do this and you will see
"//  VLista.Add(vektor);"


"The index was outside the bounds of array"

in my texfile "inlägg.txt" I have written "A b C D"
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 01:29 PM

Zero idea where you are putting that.

An index out of bounds is not the same thing as not being able to write to a file.
Was This Post Helpful? 0
  • +
  • -

#14 Realme   User is offline

  • D.I.C Head

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 01:33 PM

here is my new 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 partial class Form1 : Form
    {
        
        List<string[]> VLista = new List<string[]>();
        List<string> Spara = new List<string>();
        public static Random rng;
        

        public Form1()
        {
            
            
            InitializeComponent();
            rng = new Random();
            string[] lines = { richTextBox1.Text };

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



            foreach (string a in Spara)
            {
                string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
                
                  //  VLista.Add(vektor);
                
            }
            foreach (var b in lines)
            {
                lines = b.Split(new string[] { " " }, StringSplitOptions.None);

                VLista.Add(lines);

            }
            
        }
       
        private void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < VLista.Count; i++)
            {
                int index = rng.Next(0, i);


                textBox1.Text = Convert.ToString(VLista[index][0]); //+ VLista[index][1] + VLista[index][2] + VLista[index][3]);
            }
           
        }
        
        private void SökRuta(object sender, EventArgs e)
        {
            
        }

        
        private void Button2_Click(object sender, EventArgs e)
        {
          
            using (StreamWriter outputFile = new StreamWriter( @"C:\Users\razer\Desktop\inlägg.txt"))
            {
                    outputFile.WriteLine(richTextBox1.Text);
                
            }
            
            
        }
    }
}



in my textfile "A B C D"

the output works now but is nothing at all just empty.
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 01:38 PM

You have values in your text file... you see them. Then HOW ARE YOU CLAIMING "but is nothing at all just empty"?!
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3