FileStream.Read 方法的第二个参数称为 offset,但它是像索引一样从零开始还是从 1 开始?
免责声明:不要::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
).
我不明白. …
我正在 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) 您好,来自阿富汗,
我正在开发一个桌面应用程序,该应用程序使用肥皂消息发送和接收传真,我需要知道用户想要传真的 pdf 文件的页数。
我需要从文件中读取大量的二进制数据.我有一个固定的记录大小(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) 我面临一个错误,即“流不支持阅读”。我正在向 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 方法进行流时出现错误。我该如何解决这个错误?
我正在使用此代码将 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 传递到网站,并且我确实听到了它。
这里可能错误在哪里?
假设我有一个字符串
string hex = "55 6E 6B 6E 6F 77 6E 20 53 70 61 63 65"
Run Code Online (Sandbox Code Playgroud)
我想将这些写入文件,但它们应该是字节本身.所以结果将是未知空间.
有没有办法直接这样做,而不是先将encoding
它utf-8
写入文件?我要问的原因是,如果我首先将字符串拆分为每个值的字符,有时候utf-8
(例如0xE28780
)中的特殊字符不会很好.
非常感谢!
我正在尝试创建一个程序,将某种数据写入 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) 我有一个表单应用程序执行模拟并不断读取/写入二进制文件.如果让它通过,一切正常.但是,如果表单已关闭/模拟已中止,则文件流未正确关闭 - 文件将被锁定.有没有办法确保所有流关闭?我尝试了以下 - 但它没有效果......非常感谢,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) filestream ×10
c# ×9
binaryreader ×1
binarywriter ×1
c++ ×1
dialogresult ×1
encryption ×1
file-io ×1
file-locking ×1
http-post ×1
memorystream ×1
seek ×1
stream ×1
using ×1
utf-8 ×1