标签: filestream

FileStream.Read 的 offset 参数是否从零开始?

FileStream.Read 方法的第二个参数称为 offset,但它是像索引一样从零开始还是从 1 开始?

c# filestream

0
推荐指数
1
解决办法
990
查看次数

为什么:: feof()与:: _ eof(:: fileno())不同?

免责声明:不要::feof()用作你的循环条件. 例如,请参阅以下答案: 文件读取:二进制文件的feof()

但是,我有"真正的"代码,它演示了一个不能::feof()用作我的循环条件的问题,但是LOGICALLY,这是演示问题的最简单方法.

请考虑以下事项:我们一次迭代一个字符流:

FILE* my_file;
// ...open "my_file" for reading...

int c;
while(0 == ::feof(my_file))
{ // We are not at EOF
  c = ::getc(my_file);
  // ...process "c"
}
Run Code Online (Sandbox Code Playgroud)

上面的代码按预期工作: 文件一次处理一个char,然后EOF我们退出.

但是,以下是出乎意料的行为:

FILE* my_file;
// ...open "my_file" for reading...

int c;
while(0 == ::_eof(::fileno(my_file)))
{ // We are not at EOF
  c = ::getc(my_file);
  // ...process "c"
}
Run Code Online (Sandbox Code Playgroud)

我原以为他们会这样做. ::fileno()每次都正确返回(整数)文件描述符.但是,测试只::_eof(::fileno(my_file))运行一次,然后在第二次尝试时返回1(指示a EOF).

我不明白. …

c++ file-io stream filestream

0
推荐指数
1
解决办法
743
查看次数

永久锁定文件

我正在 VS2010 上使用 C# 开发文件锁定器/解锁器应用程序。我想要的是使用我的应用程序使用密码锁定文件,然后随时解锁。

事实上,我使用以下代码来锁定文件,但仅当应用程序仍在运行时才会锁定文件;当我关闭应用程序时,文件被解锁。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.Windows.Forms;

namespace LockFile
{
    public enum LockStatus
    {
        Unlocked,
        Locked
    }

    public class LockFilePresenter
    {
        private ILockFileView view;
        private string file2Lock = string.Empty;
        private FileStream fileLockStream = null;

        public LockFilePresenter(ILockFileView view)
        {
            this.view = view;
        }

        internal void LockFile()
        {
            if (string.IsNullOrEmpty(file2Lock) || !File.Exists(file2Lock))
            {
                view.ShowMessage("Please select a path to lock.");
                return;
            }

            if (fileLockStream != null)
            {
                view.ShowMessage("The path is already locked."); …
Run Code Online (Sandbox Code Playgroud)

c# encryption file-locking filestream

0
推荐指数
1
解决办法
1746
查看次数

如何找出用户在C#中的OpenFileDialog中选择的pdf文件的页数?

您好,来自阿富汗,
我正在开发一个桌面应用程序,该应用程序使用肥皂消息发送和接收传真,我需要知道用户想要传真的 pdf 文件的页数。

c# openfiledialog filestream dialogresult

0
推荐指数
1
解决办法
6367
查看次数

有没有比FileStream.Position更快的方式来跳过部分二进制文件

我需要从文件中读取大量的二进制数据.我有一个固定的记录大小(38),并希望一次跳过几个记录.我尝试过使用FileStrea,Position或Seek这样做,但似乎也需要花费很长时间.因此,即使我跳过10条记录 - 我也不会通过文件读取10次fatsre.

这是一个SSCCE.

调节器注意:这不是一个重复的问题,它是我从另一个问题中提取的后续内容,以便探索不同的焦点.

您需要创建2个按钮Serialize和Deserialize.

Serialize创建一个虚拟数据文件.

反序列化通过它读取.

注释掉fs.Position行以查看整个文件的原始读取.我的机器需要12秒.然后取消注释它,文件每次将跳过10条记录.希望速度提高10倍,但我的机器需要8秒.所以我假设改变fs.Position是昂贵的.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProtoBuf;
using System.IO;
using System.Diagnostics;

namespace BinTest3
{


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Serialize_Click(object sender, EventArgs e)
        {

            FileStream outBin = null;

            string binFileName = @"C:\binfile.dft";
            outBin = File.Create(binFileName, 2048, FileOptions.None);

            DateTime d = DateTime.Now;

            TickRecord tr = new TickRecord(d, 1.02, 1.03,200,300);

            for (int i …
Run Code Online (Sandbox Code Playgroud)

c# filestream seek

0
推荐指数
1
解决办法
874
查看次数

Stream 在放置 HTTP Post 请求时不支持读取

我面临一个错误,即“流不支持阅读”。我正在向 url 发出 Http post 请求。下面是我正在使用的代码

var request = (HttpWebRequest) WebRequest.Create("https://test.com/Hotel Hospitality Source?method=fetchInfo");

var postData = "&username=testing";
postData += "&password=Testing";
postData += "&hotelId=h075-103";
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using(var ms = new MemoryStream())
request.GetRequestStream().CopyTo(ms);

var response = (HttpWebResponse) request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

首先我使用了下面的代码。

 using (var stream = request.GetRequestStream())
 {
     stream.Write(data, 0, data.Length);
 }

 var response = (HttpWebResponse)request.GetResponse();

 var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

当我使用 CopyTo 方法进行流时出现错误。我该如何解决这个错误?

c# http-post filestream

0
推荐指数
1
解决办法
1万
查看次数

将 MemoryStream 保存到文件会产生 0 字节

我正在使用此代码将 MP3 MemoryStream 写入文件:

using (var nSpeakStreamAsMp3 = new MemoryStream())
using (var nWavFileReader = new WaveFileReader(nSpeakStream))
using (var nMp3Writer = new LameMP3FileWriter(nSpeakStreamAsMp3, nWavFileReader.WaveFormat, LAMEPreset.STANDARD_FAST))
{
    nWavFileReader.CopyTo(nMp3Writer);

    string sPath = "C:\\inetpub\\wwwroot\\server\\bin\\mymp3.mp3";

    using (FileStream nFile = new FileStream(sPath, FileMode.Create, System.IO.FileAccess.Write))
    {
        nSpeakStreamAsMp3.CopyTo(nFile);
    }

    sRet = (String.Concat("data:audio/mpeg;base64,", Convert.ToBase64String(nSpeakStreamAsMp3.ToArray())));
}

return sRet;
Run Code Online (Sandbox Code Playgroud)

由于某种我没有看到的原因,这会生成一个 0 字节的文件。但是,MP3 流是有效的并且可以工作。我将其作为 Base64String 传递到网站,并且我确实听到了它。

这里可能错误在哪里?

c# memorystream using filestream

0
推荐指数
1
解决办法
2128
查看次数

c# - 将包含十六进制值的字符串写为字节

假设我有一个字符串

string hex = "55 6E 6B 6E 6F 77 6E 20 53 70 61 63 65"
Run Code Online (Sandbox Code Playgroud)

我想将这些写入文件,但它们应该是字节本身.所以结果将是未知空间.

有没有办法直接这样做,而不是先将encodingutf-8写入文件?我要问的原因是,如果我首先将字符串拆分为每个值的字符,有时候utf-8(例如0xE28780)中的特殊字符不会很好.

非常感谢!

c# utf-8 filestream

0
推荐指数
1
解决办法
75
查看次数

FileStream 一直说文件正忙,但根本没有任何应用程序打开它

我正在尝试创建一个程序,将某种数据写入 C# 中的文件。当我尝试运行代码时,控制台说文件正忙,即使我根本没有打开它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace databaseProject
{
    public class Program
    {
        static void Main(string[] args)
        {
            Functions F = new Functions();

            string studentID;
            string studentName;
            string subjectA;
            string subjectB;


            Console.WriteLine("Please enter the student's ID.");
            studentID = Console.ReadLine();

            Console.WriteLine("Please enter the student's name.");
            studentName = Console.ReadLine();

            Console.WriteLine("Please enter the student's score for subject A");
            subjectA = Console.ReadLine();

            Console.WriteLine("Please enter the student's score for subject B");
            subjectB = Console.ReadLine();

            Functions.saveFile(studentID, studentName, …
Run Code Online (Sandbox Code Playgroud)

c# filestream

0
推荐指数
1
解决办法
1198
查看次数

BinaryReader或Writer.Close()没有正确关闭C#

我有一个表单应用程序执行模拟并不断读取/写入二进制文件.如果让它通过,一切正常.但是,如果表单已关闭/模拟已中止,则文件流未正确关闭 - 文件将被锁定.有没有办法确保所有流关闭?我尝试了以下 - 但它没有效果......非常感谢,T

        public BinaryWriter BinWrite;
        public BinaryReader BinRead;

        public BinaryWriter EnvBinWrite;
        public BinaryReader EnvBinRead;       

public void theForm_FormClosing(object sender, FormClosingEventArgs e)
        {

            //Close all binary file reader/writers -- crashes if it cannot overwrite files
            foreach (Building B in AllBldgs)
            {
                try
                {
                    EnvBinRead.Close();
                }
                catch
                { continue; }
                try
                {
                    EnvBinWrite.Close();
                }
                catch
                { continue; }
                try
                {
                    BinRead.Close();
                }
                catch
                { continue; }

                try
                {
                    BinWrite.Close();
                }
                catch
                { continue; }
            }
        }
Run Code Online (Sandbox Code Playgroud)

c# filestream binaryreader binarywriter

-1
推荐指数
1
解决办法
1408
查看次数