-2

Here is my txt file and i need to get x,y of O

####################
#                  #
#                  #
#                  #
#                  #
#                  #
#                  #
#   O              #
#                  #
####################
808thlife
  • 9
  • 3

3 Answers3

0

Is that the only 'O' in that file?
If so you should do the following:
have a counter Y - that will represent how many lines you iterated through. This counter will start at zero and will never be reset.
have a counter X - that will represent how many characters you iterated through in the current line. This counter will start at zero and will reset in every new line.

Flow:

int x = 0
int y = 0
for line in lines
   x = 0
   for character  in line
       if character == 'O'
         print(x,y)
       x++
   y++
Ariel Silver
  • 51
  • 1
  • 8
  • OP's question is tagged `C#` not something akin to `Basic`. –  Oct 22 '22 at 09:27
  • Read my answer.... I said "Flow:" not "Code:". Just so he has a general Idea – Ariel Silver Oct 23 '22 at 11:52
  • Read the OP's **question**. The OP said `c#` not _"Flow"._ Just so _you_ have the general idea. Compare your answer to the other two on this page to see what is different. Put yourself in the OP's shoes. Which answer would you prefer? One that is pseudo code or one that is _ready to go?_ Don't take it personally but this is your chance to improve your answer and gain possible reputation in the process. I am trying to help. –  Oct 24 '22 at 01:33
  • Let me know if you update your answer :) –  Oct 24 '22 at 04:48
0

try this code:

int yIndex = 0;
using (var fileStream = File.OpenRead("t.txt")) //t.txt your input file
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true))
{                
     String line;
//read line by line and yIndex is added each time
     while ((line = streamReader.ReadLine()) != null)
     {
        for(int i=0; i< line.Length;i++)
             if(line[i]== 'O')
             {
                 Console.WriteLine("X is: {0}    Y is:{1}",i.ToString(),yIndex);
                 return; 
             }
                yIndex++;
    }
}

result:

X is: 4 Y is:7

Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
0
            int counter = 0;
            int inlinecounter = 0;
            string line;

            var text = "O";

            System.IO.StreamReader file =
                new System.IO.StreamReader("TextFile1.txt");

            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(text))
                {
                    inlinecounter = line.IndexOf(text, 0);
                    break;
                }

                counter++;
            }

            Console.WriteLine("Line number: {0}", counter);
            Console.WriteLine("inLine number: {0}", inlinecounter);
okursan
  • 1
  • 3