Here is my txt file and i need to get x,y of O
####################
# #
# #
# #
# #
# #
# #
# O #
# #
####################
CodePudding user response:
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);
}
yIndex ;
}
}
result:
X is: 4 Y is:7
CodePudding user response:
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);
CodePudding user response:
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