5 Replies - 930 Views - Last Post: 20 August 2016 - 07:43 PM

#1 viktor80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 25-April 16

Avoid writing of existing line but find next row through the index of

Posted 12 August 2016 - 04:48 PM

Hello, I’m new with C# need your help to figure out

In this code, I insert a new line and write it into the text document with each loop of endless cycle and keeping data.

What to do, if I want avoid writing of already exist line, but need to find the line which is a previous or next from current found line, in dependency of exist line index. If index number is indivisible, make +1 to it and show me the next line from current. if index is divisible -1, show me previous. For clarity, for example text document content:


Line1 1 is indivisible index, show me Line2
Line2 2 is divisible index, show me Line1
Line3 3 is indivisible index, show me Line4
Line4 4 is divisible index, show me Line3
Line5 5 is indivisible index, show me Line6

Guess need here also somehow to avid empty index, so here is my code:


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

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

            int i = 0;

            for (;;)/>
            {

                Console.Write("Write text: ");
                var row = Console.ReadLine();

                Console.WriteLine(i++ + ". " + (row));

                TextWriter tsw = new StreamWriter("lines.txt", true);

                tsw.WriteLine(row);

                tsw.Close();

            }

            Console.Read();
        }
    }
}

This post has been edited by viktor80: 12 August 2016 - 04:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Avoid writing of existing line but find next row through the index of

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,705
  • Joined: 18-April 07

Re: Avoid writing of existing line but find next row through the index of

Posted 12 August 2016 - 08:25 PM

Are these lines in the text all the same size? What you are essentially needing is a random access read file. If you know that each row is the same size, you could use a FileStream object and use its seek() method to seek to the start of a previous line or the start of the next line.

Another way you could do this, if the file isn't too big, is to read all lines into something like an array and use the array's indexes to look at the previous line or the next line.

Yet another way is a slight take on the second way and just keep track of a few lines at all times... like a previous, current and next line. While reading and setting "current" you can then go to previous or next and read those lines. Now this assumes that you won't read previous and say "Oh that too is divisible, go to the previous of the previous line".

So there are some options. Hope those ideas help. :)
Was This Post Helpful? 1
  • +
  • -

#3 viktor80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 25-April 16

Re: Avoid writing of existing line but find next row through the index of

Posted 13 August 2016 - 04:01 AM

Hello, thanks for feedback. No lines is different, in some case is a just one word in another long phrase with wight spaces. I thin the best way for me somehow figure out through the index, but I have no idea how...

This post has been edited by andrewsw: 13 August 2016 - 01:05 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,251
  • Joined: 05-May 12

Re: Avoid writing of existing line but find next row through the index of

Posted 16 August 2016 - 06:03 AM

When you say divisible or indivisisable, divisable or indivisable by what? Or are you simply talking about odd vs. even numbers? (e.g. even numbers are divisable by 2, and odd numbers are indivisable by 2.)
Was This Post Helpful? 0
  • +
  • -

#5 viktor80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 25-April 16

Re: Avoid writing of existing line but find next row through the index of

Posted 19 August 2016 - 07:06 AM

Hello thank you for your feedback, yes exactly odd and even number, but question is solved.

This post has been edited by Skydiver: 20 August 2016 - 07:03 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,251
  • Joined: 05-May 12

Re: Avoid writing of existing line but find next row through the index of

Posted 20 August 2016 - 07:43 PM

There is no need to quote the post above yours. Just just the big Reply button, or the Fast Reply area.

Do you care to share your solution so that others who stumble on this thread can also learn?

Personally, the approach I would have used would have been to create an extension method like this:
IEnumerable<T> SwapPairs(this IEnumerable<T> items)
{
    bool odd = true;
    T oddItem = default(T);
    foreach (var item in items)
    {
        if (odd)
        {
            oddItem = item;
        }
        else
        {
            yield return item;
            yield return oddItem;
        }
        odd = !odd;
    }

    if (odd)
        yield return oddItem;
} 



and then simply done File.ReadLines(fileName).SwapPairs().Distinct().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1