using System;
using System.IO;
class leerPBM
{
public static void Main(string[] args)
{
string fileName;
string data;
string line;
int width;
int height;
Console.WriteLine("Enter the file name: ");
fileName = Console.ReadLine();
if (!fileName.Contains(".pbm"))
fileName += ".pbm";
if (!File.Exists(fileName))
{
Console.WriteLine("File not found!");
return;
}
// File reading
try
{
StreamReader myFile = File.OpenText(fileName);
line = myFile.ReadLine();
if (line != "P1")
{
Console.WriteLine("Does not seem a B&W ASCII PBM");
return;
}
Console.WriteLine("Found B&W ASCII PBM");
// Width and height
line = myFile.ReadLine();
if ((line.Length > 1) && (line[0] == '#'))
{
Console.WriteLine("Comment: "
+ line.Substring(1));
line = myFile.ReadLine();
}
string[] widthheight = line.Split(' ');
width = Convert.ToInt32(widthheight[0]);
height = Convert.ToInt32(widthheight[1]);
Console.WriteLine("width: {0}", width);
Console.WriteLine("height: {0}", height);
// Data reading (gathering all the lines in one)
data = "";
line = myFile.ReadLine();
while (line != null)
{
data += line;
line = myFile.ReadLine();
}
myFile.Close();
}
catch (IOException problem)
{
Console.WriteLine("File was not read properly");
Console.WriteLine("Details: {0}", problem.Message);
return;
}
// Delete spaces, so that ony 0 and 1 remain
data = data.Replace(" ", "");
// And traversing the data, displaying them
int pos = 0;
foreach (char symbol in data)
{
// After the width, we must advance to the next line
if (pos % width == 0)
Console.WriteLine();
if (symbol == '1')
Console.Write("X");
else if (symbol == '0')
Console.Write(".");
pos++;
}
Console.WriteLine();
}
}