streamreader and writer into same textfile

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

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

#16 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:40 PM

no no no. I mean I have textfile in that textfile it is written "A B C D"

but in the windows form when running it just shows empty space. No text written at all
Was This Post Helpful? 0
  • +
  • -

#17 modi123_1   User is offline

  • Suitor #2
  • member icon



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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 01:46 PM

Put a breakpoint at the top of your functions and step through it. See what is being filled when, where, and track the values.
Was This Post Helpful? 0
  • +
  • -

#18 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:56 PM

I tried and I don't get it, I am not so used to step through.

Can you give hind on what is happening with my text and why it does not show up?

I have showed alot of effort and I have single handily made this code. You can give himself not against site rules
Was This Post Helpful? 0
  • +
  • -

#19 modi123_1   User is offline

  • Suitor #2
  • member icon



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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 02:00 PM

You wear me out trying to guess what is actually wrong with a giant mess of spaghetti code. I'll pass for now.
Was This Post Helpful? 0
  • +
  • -

#20 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 - 02:01 PM

Anyone else??
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 02:43 PM

View PostRealme, on 11 June 2019 - 04:40 PM, said:

but in the windows form when running it just shows empty space. No text written at all

This is why we asked you for a minimal compilable code. With your current spaghetti code, it's very hard to guess at what is happening. To make matters worse, you just posted the part of the code, but not the rest. How the heck are we supposed to know what is happening?

Anyway, if I had to guess, the empty space you are talking about is what is being displayed on your form that you have call textbox1. If that guess is correct, then the reason why it is empty is because VLista[0][0] happens to be an empty string. And the reason of it always selecting index == 0 would be because VLista.Count == 1. And the reason for VLista only containing a single array is because your RichTextBox1 contains some string that looks like " A B C D". Note the leading space before the 'A' in that string.
Was This Post Helpful? 0
  • +
  • -

#22 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 - 02:51 PM

Hi, I was drawing the same conclusion and I checked now and there is no space before "A".

Someone said it might be because streamreader is not containing "using" and writer have no "true" in it.

I fixed true but using can't add as "file" does not reach the name.

However I have posted the whole code here. Last post is the whole coding itself.
Was This Post Helpful? 0
  • +
  • -

#23 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 03:00 PM

Here is the minimal compilable code to reproduce the problem, assuming there is a leading space in that RichTextBox:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleCS
{
    class Program
    {
        static void DumpStringArray(string name, string [] arr)
        {
            int i = 0;
            foreach(var item in arr)
            {
                Console.WriteLine($"{name}[{i}] = '{arr[i]}'");
                i++;
            }
        }

        static void DumpStringArrayList(string name, List<string []> list)
        {
            int i = 0;
            foreach (var arr in list)
                DumpStringArray($"{name}[i++]", arr);
        }

        static void Main()
        {
            var input = " A B C D";
            var tokens = input.Split(new string [] { " " }, StringSplitOptions.None);
            DumpStringArray(nameof(tokens), tokens);

            var list = new List<string[]>();
            list.Add(tokens);
            DumpStringArrayList(nameof(list), list);

            var random = new Random();
            var index = random.Next(0, list.Count);

            Console.WriteLine($"index = {index}");
            Console.WriteLine($"list[{index}][0] = '{list[index][0]}'");
        }
    }
}



which produces the following output:
tokens[0] = ''
tokens[1] = 'A'
tokens[2] = 'B'
tokens[3] = 'C'
tokens[4] = 'D'
list[i++][0] = ''
list[i++][1] = 'A'
list[i++][2] = 'B'
list[i++][3] = 'C'
list[i++][4] = 'D'
index = 0
list[0][0] = ''



View PostRealme, on 11 June 2019 - 05:51 PM, said:

However I have posted the whole code here. Last post is the whole coding itself.

No, it is not. You need to also post the Program.cs, as well as the Form1.Designer.cs for us to have any hope of compiling and reproducing your issue.
Was This Post Helpful? 0
  • +
  • -

#24 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 - 03:00 PM

Okay I found out that button 1 has textbox and not richtextbox which we all missed so far. About this skydiver you pointed out I will check this out and reply back
Was This Post Helpful? 0
  • +
  • -

#25 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 03:01 PM

View PostRealme, on 11 June 2019 - 05:51 PM, said:

Hi, I was drawing the same conclusion and I checked now and there is no space before "A".

There is no need for you to try to draw any such conclusions. If you step through your code with a debugger you will definitely know without a doubt since you'll see exactly what the values of the variables are.

Take time to learn how to use the Visual Studio IDE. It is a very powerful debugger right there at your fingertips. It can help you figure out exactly what is going wrong.
Was This Post Helpful? 0
  • +
  • -

#26 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 - 03:32 PM

Okay I checked now. The array list is empty as you said skydiver. Also I realized I don't even have streamreader for the inlägg.txt file....


Isn't that the problem??
Was This Post Helpful? 0
  • +
  • -

#27 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: streamreader and writer into same textfile

Posted 11 June 2019 - 03:44 PM

Your lines 30-46 from post #24 effectively don't do anything with the lines you read in from your file. Given that, it doesn't really matter if you have a streamreader or not since it about as effective as shooting marshmallows at Iron Man.
Was This Post Helpful? 0
  • +
  • -

#28 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 - 10:55 PM

How can I upload the file so you guys see?
Was This Post Helpful? 0
  • +
  • -

#29 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 12 June 2019 - 01:00 AM

I tried and the "lines" were empty
Was This Post Helpful? 0
  • +
  • -

#30 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: streamreader and writer into same textfile

Posted 12 June 2019 - 06:35 AM

View PostRealme, on 12 June 2019 - 01:55 AM, said:

How can I upload the file so you guys see?

You don't need to upload. You can simply copy and paste the contents into code tags the same way you have been doing for your Form1.cs.

You should really be creating a new project with the minimal code needed to reproduce the problem. As your programs get bigger and bigger, it will become more and more untenable to keep posting all of your code. Personally, I've found creating a project for minimal reproduction also tends to be very educational and productive. About half the time, I end up discovering and solving the problem myself during this phase. (The other half of the time, it let's me see where I can improve the code later once I hopefully figure out what the problem is.)

As another aside, this is also where following the SOLID principles of Object Oriented Design pays off. If you followed the principle, it'll be very easy to pull out just the minimal pieces needed reproduce the problem and drop them into another project.
Was This Post Helpful? 0
  • +
  • -

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