0

I am trying to read a file letter by letter however my various attempts do not work and continue to find only one time my letter in the file

string lettre = "T";
string[] file = File.ReadAllLines("C:/<pathto>/test7.txt");

for (int i = 0; i < file.Length;i++)
{
    if (file[i].Contains(lettre))
    {
        Console.WriteLine("test");
    }
}

I try to read the file letter by letter to generate pixels but I block on reading the file

Fildor
  • 14,510
  • 4
  • 35
  • 67
canterlot
  • 19
  • 2
  • 2
    Reading letter by letter can become difficult. How about reading byte by byte? Is it ASCII only? – Thomas Weller Jun 23 '21 at 13:12
  • Look at this question + answer https://stackoverflow.com/questions/16618341/how-to-read-character-in-a-file-1-by-1-c-sharp – Vash Jun 23 '21 at 13:13
  • Just use equal : if (file[i] == lettre) – jdweng Jun 23 '21 at 13:15
  • You can create a [StreamReaderr](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.-ctor?view=net-5.0#System_IO_StreamReader__ctor_System_String_) using the constructor that accepts a path and read the file character by character using [Read](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.read?view=net-5.0). If the file is small though, you can just read all of it in memory as a string with `var text=File.ReadAllText(path);` and read the characters, eg `foreach(var c in text){...}` or `for (int i=0;i – Panagiotis Kanavos Jun 23 '21 at 13:17
  • @canterlot what are you trying to do anyway? Find if the file contains a single T? How many Ts there are? Which lines have a T? All of these have different, simple solutions, and none requires reading character by character. The code you posted will find all lines that have a T in them – Panagiotis Kanavos Jun 23 '21 at 13:19
  • Try `string text = File.ReadAllText()` and then `foreach(char c in text) { }` – Aleksa Ristic Jun 23 '21 at 13:24
  • _I block on reading the file_ Meaning what? The app is blocking or your head? – TaW Jun 23 '21 at 13:30

2 Answers2

1

From your code it looks as if it's fine for you to read the whole file into memory. If so, it's just a matter of looping.

Also note the difference between the string "T" and the character (letter) 'T'.

string[] file = File.ReadAllLines("C:/<pathto>/test7.txt");

foreach(string line in file)
{
    foreach(char letter in line)
    {
         if (letter == 'T')
         {
                // do something
         }
    }
}

If you really want to read the file character by character, it's also fine if the characters are from ASCII range only. You can use FileStream.ReadByte()

using(var fileStream = new FileStream(fileName, FileMode.Open))
{
    int character = 0;
    while(character != -1)
    {
         character = fileStream.ReadByte();
         if (character == -1) continue;
         if ((char)character == 'T')
         {
              // do something
         }
    }
}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Just for clarification ASCII just makes things easier or is eg. UTF8 impossible to do? – Rand Random Jun 23 '21 at 13:38
  • 1
    @RandRandom ASCII is one byte per character. UTF-8 is one to four bytes per *code point*, and then it may be several code points for a single "logical" character (combined emojis are the common example). The C#'s `char` is the code point, not a logical character. For enumerating "logical" characters, [further effort](https://stackoverflow.com/q/14115503/11683) is required. – GSerg Jun 23 '21 at 13:50
1

This should work:

using System;
using System.IO;
using System.Text;

namespace Exemple
{
    class Program
    {
        static void Main(string[] args)
        {
            char lettre = 'T';

            using(StreamReader file = new StreamReader(@"C:\<pathto>\test7.txt", Encoding.UTF8)) //you can change the Encoding file type
            {
                while (!file.EndOfStream)
                {
                    string line = file.ReadLine();
                    foreach(char letter in line)
                    {
                        if(letter == lettre)
                            Console.WriteLine("An letter " + lettre + " founded.");
                    }
                }
            }
        }
    }
}

Also take care on your OS, if is Windows the path separator is the '\', if is a Unix OS is the '/'.