4

I am reading in from a text file using StreamReader into a string array text[]. One of the lines in the textfile is being read in as "\0" in positions 1 -> 20 of the array. How would I go about detecting this null character and ignoring this line.

Code example:

StreamReader sr = new StreamReader(Convert.ToString(openFileDialog1.FileName));
while (!sr.EndOfStream)
{
    string l= sr.ReadLine();
    string[] parsedLine = l.Split(new char[] { '=' },StringSplitOptions.RemoveEmptyEntries);
    // Not working:
    if (parsedLine.Length == 0)
    {
        MessageBox.Show("Ignoring line");
    }

Any help would be great!

Daniel Flannery
  • 1,166
  • 8
  • 23
  • 51

5 Answers5

3

Assuming you mean a character with ascii code: 0

   if (parsedLine.Length == 0 || parsedLine[0] == '\0')continue;

edit the above will work if parsedLine is a string, but for the parsing in your code:

    string[] parsedLine = l.Split(new char[] { '=' },StringSplitOptions.RemoveEmptyEntries)
                      .Where(s=>s.Length != 1 || s[0] != '\0').ToArray();
Me.Name
  • 12,259
  • 3
  • 31
  • 48
  • Does not work. Operator "==" cannot be applied to operands of type 'string' and 'char' – Daniel Flannery Jun 21 '12 at 10:16
  • @L337BEAN Oops, sorry, didn't read correctly, thought parsedLine was a read in string, added a linq alternative for the string array. – Me.Name Jun 21 '12 at 10:21
  • This is working to a certain extent. The problem now is that It is removing everything from the string each time. For example if `l` contains `H/0e/0l/0l/0o/0` after your line of code its passing l to parsedLine as `""`. I would like to keep the `hello` – Daniel Flannery Jun 21 '12 at 10:30
  • hmm, this may be due to a space in the logfile. Ill check on it. – Daniel Flannery Jun 21 '12 at 10:33
  • The logfile is riddles with null characters. The programs is picking up spaces as them and saving them null characters. Only way I can see to get around this is to check if the first character of l is null and the second character is `=` – Daniel Flannery Jun 21 '12 at 10:45
  • 2
    @L337BEAN So it's more a matter of removing all occurences of \0 than to remove entries where only a \0 is entered? In that case you could use l = l.Replace("\0", " ") to replace with spaces or l = l.Replace("\0", ""); to remove them altogether. Would that be viable? – Me.Name Jun 21 '12 at 11:14
  • Well I want to remove empty lines ( which I can do ), and lines thats first character is `\0` and second character `=`. These are being used as breaks in the logfile. eg: `= = = = = = = =` is being read as `=\0=\0=\0=\0=\0=\0=\0=\0` – Daniel Flannery Jun 21 '12 at 11:57
  • @L337BEAN For that example, the 2nd code sample should work, but I think the replace mentioned before still is easiest: string l= sr.ReadLine().Replace("\0",""); For the last example that would create a lot of empty elements after the split, but they would be removed with the RemoveEmtpyEntries clause. If the replace is not a viable solution, could you post (or upload) a longer sample file with the different scenarios? – Me.Name Jun 21 '12 at 12:14
  • The trouble is that `line.Replace("\0", "");` is not working. printing the string after running this still shows the `\0` as spaces. – Daniel Flannery Jun 21 '12 at 12:20
  • Wait. I believe im using it wrong. Update: yes, your correct. this is working. – Daniel Flannery Jun 21 '12 at 12:24
1

Use the built-in String.IsNullOrEmpty() method:

if (!string.IsNullOrEmpty(l))
{
    // your code here
}
Bobby
  • 11,419
  • 5
  • 44
  • 69
Estefany Velez
  • 328
  • 4
  • 18
1

Just ignore the line if it contains the null character.

string l = sr.ReadLine();
if (string.IsNullOrEmpty(l) || l[0] == '\0'))
   continue;
...
bruno conde
  • 47,767
  • 15
  • 98
  • 117
1

Here is a linq solution that should work:

StreamReader sr = new StreamReader(Convert.ToString(openFileDialog1.FileName));
while (!sr.EndOfStream)
{
    string l= sr.ReadLine();
    bool nullPresent = l.ToCharArray().Any(x => x.CompareTo('\0') == 0);

    if (nullPresent)
    {
        MessageBox.Show("Ignoring line");
    }
    else
    {
        // do other stuff
    }
}
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
0
string l= sr.ReadLine();
if (l == "") {
  MessageBox.Show("Ignoring line");
  continue;
}
xdazz
  • 158,678
  • 38
  • 247
  • 274