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]+" ");
}
}
}
}
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] + " ");
}
}
是的,我刚刚运行它,因为我怀疑你的循环后需要以下内容:
tw.Close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.