0

I'm trying to read the values of pixels from text file and generate an image file. But first I want to make sure that I can read all of the values in the file. I use this code, but the output misses some of the integers from the input file-the last ones. I don't know why! Can you help me? Here's the code:

namespace txtToImg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            TextWriter tw = new StreamWriter("D:\\out.txt");
            string fileContent = File.ReadAllText("D:\\in.txt");

            string[] integerStrings = fileContent.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            int[] integers = new int[integerStrings.Length];
            //tw.Write(integerStrings.Length);

            for (int n = 0; n < integerStrings.Length; n++)
            {
                integers[n] = int.Parse(integerStrings[n]);

                tw.Write(integers[n]+" ");


            }
        }
    }
}
9
  • 2
    I notice the StreamWriter object doesn't appear to be getting closed. Not sure if that would make any difference, but its the only obvious thing I can see. Commented May 17, 2013 at 19:40
  • You sure that shouldn't be fileContent.Split(new char[] { ' ', '\n' }, ... ? Commented May 17, 2013 at 19:43
  • My integers are split by space, I don't need '\n' or ','. Thanks though! Commented May 17, 2013 at 19:46
  • Andrew, Thank you so much! I closed the StreamWriter object tw and it worked! I do not know why, but it works perfectly now! Thank you again! Commented May 17, 2013 at 19:48
  • @AnaZlateva The best way is to use using as in Bearcat's suggestion below. Then it will get closed automatically, even if an exception occurs. Commented May 17, 2013 at 19:50

2 Answers 2

2

I agree with Andrew's comment your streamWriter object isn't getting closed so for one thing I would try this. I made the change below and attempted what you are doing and my output file contained all the entries.

 using (TextWriter tw = new StreamWriter(@"D:\out.txt"))
        {
            string fileContent = File.ReadAllText(@"D:\in.txt");

            string[] integerStrings = fileContent.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            int[] integers = new int[integerStrings.Length];

            for (int n = 0; n < integerStrings.Length; n++)
            {
                integers[n] = int.Parse(integerStrings[n]);
                tw.Write(integers[n] + " ");
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Ana if this answer has solved your problem , don't forget to choose it as your question's answer
0

Right, I've just ran it and as I suspected you need the following after your loop:

tw.Close();

1 Comment

Like Andrew suggested I would wrap your TextWriter in a using statement. It Implements IDiposable so after the closing brace tw is properly disposed, and your out file then is release and accessible to other processes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.