编写一个文件,添加随机字符到每行的开头(Writing a file adding random characters to start of each line)

编程入门 行业动态 更新时间:2024-10-26 15:28:33
编写一个文件,添加随机字符到每行的开头(Writing a file adding random characters to start of each line)

我在Windows Phone 7中使用C#覆盖文件。当我这样做时,每行的开头都会添加一个看似随机的字符。

这是为什么发生?

码:

public static bool overwriteFile(string filename, string[] inputArray) { try { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); FileStream stream = store.OpenFile(filename, FileMode.Create); BinaryWriter writer = new BinaryWriter(stream); foreach (string input in inputArray) { writer.Write(input + "\n"); } writer.Close(); return true; } catch (IOException ex) { return false; } }

洛代码:

public static Idea[] getFile(string filename) { try { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); string fileContents = null; if (store.FileExists(filename)) // Check if file exists { IsolatedStorageFileStream save = new IsolatedStorageFileStream(filename, FileMode.Open, store); StreamReader streamReader = new StreamReader(save); fileContents = streamReader.ReadToEnd(); save.Close(); } string[] lines = null; if (fileContents != null) { lines = fileContents.Split('\n'); } Idea[] ideaList = null; if (lines != null) { ideaList = new Idea[lines.Length]; for (int i = 0; i < lines.Length; i++) { ideaList[i] = new Idea(lines[i].TrimEnd('\r')); } } return ideaList; } catch (IOException ex) { return null; } }

I'm overwriting a file using C# in Windows Phone 7. When I do this a seemingly random character is added to the start of each line.

Why is this happening?

Code:

public static bool overwriteFile(string filename, string[] inputArray) { try { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); FileStream stream = store.OpenFile(filename, FileMode.Create); BinaryWriter writer = new BinaryWriter(stream); foreach (string input in inputArray) { writer.Write(input + "\n"); } writer.Close(); return true; } catch (IOException ex) { return false; } }

Lodaing Code:

public static Idea[] getFile(string filename) { try { IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); string fileContents = null; if (store.FileExists(filename)) // Check if file exists { IsolatedStorageFileStream save = new IsolatedStorageFileStream(filename, FileMode.Open, store); StreamReader streamReader = new StreamReader(save); fileContents = streamReader.ReadToEnd(); save.Close(); } string[] lines = null; if (fileContents != null) { lines = fileContents.Split('\n'); } Idea[] ideaList = null; if (lines != null) { ideaList = new Idea[lines.Length]; for (int i = 0; i < lines.Length; i++) { ideaList[i] = new Idea(lines[i].TrimEnd('\r')); } } return ideaList; } catch (IOException ex) { return null; } }

最满意答案

随机字符是长度前缀; 请参阅http://msdn.microsoft.com/en-us/library/yzxa6408.aspx 。

您应该使用某种类型的TextWriter将字符串写入文件; 不是BinaryWriter。

StreamWriter可能是最好的,然后你可以使用WriteLine方法。

The random character is a length prefix; see http://msdn.microsoft.com/en-us/library/yzxa6408.aspx.

You should be using some type of TextWriter to write strings to the file; NOT a BinaryWriter.

A StreamWriter might be the best and then you could use the WriteLine method.

更多推荐

本文发布于:2023-07-29 15:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:开头   字符   文件   Writing   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!