试图写入文件 [英] Trying to write to file

查看:64
本文介绍了试图写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>// I AM TRYING TO WRITE AND CREATE A FILE, USING ONLY ONE BIT AT A TIME.
// I AM WRITING THE FILE, BUT NOTHING GOES IN.
// ALSO I GETTING ERRORS FOR BEING OUTSIDE OF THE ARRAY.
using System;
using System.IO;
using System.Collections;
namespace Applica
{
    static class Program
    {
        static void Main(string[] args)
        {
            bool tif = true;
            byte[] buffer = BitConverter.GetBytes(tif);
            FileInfo ap = new FileInfo("tempii.txt");
            string filePath = ap.FullName;
            string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
            using (Stream output = File.OpenWrite(destinationPath))
            {
                int bits = 32;
                while (bits > 0)
                {
                    for (int i = 1; i < 33; i++)//4 random bytes
                    {
                        if (tif == true)
                        {
                            tif = false;
                            goto A;
                        }
                        if (tif == false) tif = true;
                        A:;
                        output.Write(buffer, 0, bits);
                    }
                }
            }
        }
    }
}











我的尝试:



我正在尝试编写并创建一个文件,一次只使用一个布尔位。我似乎无法将boolean位放入生成该文件的字节数组中。






What I have tried:

I am trying to write and create a file, using only one boolean bit at a time. I cant seem to get the boolean bits into the byte array that makes the file.

推荐答案

使用调试器。



您将收到以下错误消息;

错误: mscorlib.dll中出现未处理的System.ArgumentException类型异常

附加信息:偏移量和长度超出了数组的范围,或者数量大于从索引到源集合末尾的元素数量



错误将由该行引起;

Use your debugger.

You will be receiving the following error message;
Error: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Offset and length were out of bounds for the array or the count is greater that the number of elements from the index to the end of the source collection

The error will be caused by the line;
output.Write(buffer, 0, bits);



根据MSDN文档 - Stream.Write方法(字节[],Int32,Int32)(System.IO) [ ^ ] - Stream.Write方法接受一个字节数组(缓冲区),一个开始写入的位置(在你的情况下是位置0)&写入的字节数(在您的情况下为32)。

您传递的字节数组始终具有 1个字节的长度,因此您无法写入32个字节...



此外, goto 语句在大多数人看来都是糟糕的编程。通常你会用它来打破深度嵌套的循环和放大器。在大多数情况下你可能会退出到另一种方法



亲切的问候


As per the MSDN documentation - Stream.Write Method (Byte[], Int32, Int32) (System.IO)[^] - the Stream.Write method accepts an array of bytes (buffer), a position to start writing from (position 0 in your case) & the number of bytes to write (32 in your case).
The Byte array you have passed has a length of 1 bytes at all times, hence you cannot write 32 bytes...

Additionally, goto statements are poor programming in most people's opinion. Typically you would use it to break out of a deeply nested loop & in most cases you would probably just exit to another method

Kind Regards


这个循环

This loop
for (int i = 1; i < 33; i++)//4 random bytes
{
    if (tif == true)
    {
        tif = false;
        goto A;
    }
    if (tif == false) tif = true;
    A:;
    output.Write(buffer, 0, bits);
}



是糟糕的编程,更好的风格是


is poor programming, a better style is

for (int i = 1; i < 33; i++)//4 random bytes
{
    if (tif == true)
    {
        tif = false;
    }
    elseif (tif == false)
    {
        tif = true;
    }
    output.Write(buffer, 0, bits);
}



可以简化为


and can be simplified as

for (int i = 1; i < 33; i++)//4 random bytes
{
    output.Write(buffer, 0, bits);
}





有一个工具可以让你看到你的代码在做什么,它的名字是调试器的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

在Visual中调试C#代码工作室 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不是'找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。



There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于试图写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆