我正在用C#将文本写入二进制文件,并看到写入字符串和字符数组之间的数量差异.我正在使用System.IO.BinaryWriter并在写入时观察BinaryWriter.BaseStream.Length.这些是我的结果:
using(BinaryWriter bw = new BinaryWriter(File.Open(“data.dat”), Encoding.ASCII))
{
string value = “Foo”;
// Writes 4 bytes
bw.Write(value);
// Writes 3 bytes
bw.Write(value.ToCharArray());
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么当我只写3个ASCII字符时,字符串重载会写入4个字节.有谁能解释一下?
我正在运行一个安装了Git(最新来自Ubuntu软件包管理器)的Ubuntu(9.10)服务器.通过SSH访问Git.在Windows机器上,我使用Cygwin来推/拉代码.
我可以将我的项目代码推送到服务器上,但是当我执行克隆或拉取时,它会返回[致命:早期EOF]错误,大约为75-80%.
经过进一步调查,看起来文本数据在拉/克隆时没有问题,但是当从Git中提取jar文件和图像时,将发生错误.
有任何建议/建议可以帮助解决这个问题吗?
提前致谢.
在我使用以下代码片段的实验中,我没有找到任何特别的区别,无论我是否使用ios创建流:二进制模式:
int main()
{
ifstream ostr("Main.cpp", ios::in | ios::binary | ios::ate);
if (ostr.is_open())
{
int size = ostr.tellg();
char * memBlock = new char[size + 1];
ostr.seekg(0, ios::beg);
ostr.read(memBlock, size);
memBlock[size] = '\0';
ofstream file("trip.cpp", ios::out | ios::binary);
file.write(memBlock, size);
ostr.close();
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我试图将原始源文件复制到另一个具有不同名称的文件中.
我的问题是当使用/不使用ios :: binary模式打开fstream对象时,读/写调用(与二进制文件IO相关)之间的区别是什么?使用二进制模式有什么好处吗?什么时候何时何时不使用它来做文件IO?
我正在尝试将二进制文件读入R,但是该文件具有用二进制代码编写的数据行。因此,它没有属于一列的完整数据集,而是存储为数据行。这是我的数据:
字节1-4:int ID字节5:char响应字符字节6-9:int Resp Dollars字节10:char类型char
任何人都可以帮助我弄清楚如何将此文件读入R?
嗨,大家好,
这是到目前为止我测试过的代码。我尝试了几件事,但收效甚微。不幸的是,我不能将任何数据发布在公共网站上,抱歉。我是R的新手,因此在如何改进代码方面需要一些帮助。提前致谢。
> binfile = file("File Location", "rb")
> IDvals = readBin(binfile, integer(), size=4, endian = "little")
> Responsevals = readBin(binfile, character (), size = 5)
> ResponseDollarsvals = readBin (binfile, integer (), size = 9, endian= "little")
Error in readBin(binfile, integer(), size = 9, endian = "little") :
size 9 is unknown on this machine
> Typevals = readBin (binfile, character (), size=4)
> binfile1= cbind(IDvals, Responsevals, ResponseDollarsvals, Typevals)
> dimnames(binfile1)[[2]]
[1] "IDvals" …Run Code Online (Sandbox Code Playgroud) 我想读取几MB的二进制文件的内容并将其存储到缓冲区中.这是我的函数原型(如果需要我可以更改它):
procedure GET_BIN_CONTENT_FROM_PATH(PATH : in UNBOUNDED_STRING;
CONTENT : out UNBOUNDED_STRING);
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了两种方法,都使用Direct_IO包.在第一种方法中,我正逐字逐句地阅读文件; 它工作,但它非常缓慢.为了加快这个过程,我尝试用MB读取文件MB:
procedure GET_BIN_CONTENT_FROM_PATH (PATH : in UNBOUNDED_STRING;
CONTENT : out UNBOUNDED_STRING) is
BIN_SIZE_LIMIT : constant NATURAL := 1000000;
subtype FILE_STRING is STRING (1 .. BIN_SIZE_LIMIT);
package FILE_STRING_IO is new ADA.DIRECT_IO (FILE_STRING);
FILE : FILE_STRING_IO.FILE_TYPE;
BUFFER : FILE_STRING;
begin
FILE_STRING_IO.OPEN (FILE, MODE => FILE_STRING_IO.IN_FILE,
NAME => TO_STRING (C_BASE_DIR & PATH));
while not FILE_STRING_IO.END_OF_FILE (FILE) loop
FILE_STRING_IO.READ (FILE, ITEM => BUFFER);
APPEND (CONTENT, BUFFER);
end loop;
FILE_STRING_IO.CLOSE (FILE);
end GET_BIN_CONTENT_FROM_PATH;
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果文件中剩余少于1MB,似乎不会发生READ操作.结果,大文件(> …
我有一个已知格式/结构的二进制文件.
如何将所有二进制数据读入结构数组?
像(在伪代码中)
bytes = read_file(filename)
struct = {'int','int','float','byte[255]'}
data = read_as_struct(bytes, struct)
data[1]
>>> 10,11,10.1,Arr[255]
Run Code Online (Sandbox Code Playgroud)
编辑:
目前解决方案:
data = []
fmt = '=iiiii256i'
fmt_s = '=iiiii'
fmt_spec = '256i'
struct_size = struct.calcsize(fmt)
for i in range(struct_size, len(bytes)-struct_size, struct_size):
dat1= list(struct.unpack(fmt_s, bytes[i-struct_size:i-1024]))
dat2= list(struct.unpack(fmt_spec, bytes[i-1024:i]))
dat1.append(dat2)
data.append(dat1)
Run Code Online (Sandbox Code Playgroud) 在我继续我的程序之前,我有一个需要解决的大问题.
我必须打开一个二进制文件,读取它的内容,将内容保存到缓冲区,使用malloc在堆上分配空间,关闭文件,最后是printf(.bin文件的内容).我到目前为止(关闭文件尚未实现):
void executeFile(char *path){
FILE *fp; /*filepointer*/
size_t size; /*filesize*/
unsigned int buffer []; /*buffer*/
fp = fopen(path,"rb"); /*open file*/
fseek(fp, 0, SEEK_END);
size = ftell(fp); /*calc the size needed*/
fseek(fp, 0, SEEK_SET);
buffer = malloc(size); /*allocalte space on heap*/
if (fp == NULL){ /*ERROR detection if file == empty*/
printf("Error: There was an Error reading the file %s \n", path);
exit(1);
}
else if (fread(&buffer, sizeof(unsigned int), size, fp) != size){ /* if count of read bytes != …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用django 1.7提供PDF文件,这基本上是"应该"工作的代码......如果我将content_type更改为'text'并使用它下载.tex文件,它肯定有效,但是我用二进制文件尝试它,我得到"/Dev/to/file/filename.pdf中的UnicodeDecodeError'utf-8'编解码器无法解码位置10中的字节0xd0:无效的连续字节"
def download(request, file_name):
file = open('path/to/file/{}'.format(file_name), 'r')
response = HttpResponse(file, content_type='application/pdf')
response['Content-Disposition'] = "attachment; filename={}".format(file_name)
return response
Run Code Online (Sandbox Code Playgroud)
所以基本上,如果我理解正确,它会尝试将文件作为UTF-8编码的文本文件而不是二进制文件.我试图将content_type更改为'application/octet-stream',结果类似.我错过了什么?
我们经常使用fgetc这样的:
int c;
while ((c = fgetc(file)) != EOF)
{
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
从理论上讲,如果文件中的某个字节的值为EOF,则此代码存在错误 - 它会提前中断循环并且无法处理整个文件.这种情况可能吗?
据我所知,fgetc内部将从文件读取的字节转换为unsigned char然后int返回,并返回它.如果范围int大于的范围,这将起作用unsigned char.
如果不是(可能那么sizeof(int)=1)会发生什么?
fgetc读取一个等于EOF文件的合法数据吗?EOF吗?fgetc是一个未实现的功能?EOF是另一种类型long吗?我可以通过额外的检查使我的代码变得简单:
int c;
for (;;)
{
c = fgetc(file);
if (feof(file))
break;
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
如果我想要最大的便携性,这是必要的吗?
我想将二进制文件中的二进制数据提取到字节数组中.我很难让它正常工作.
你可以在这里看到jsFiddle:https://jsfiddle.net/alexsuch/6aG4x/
HTML:
<div ng-controller="MainCtrl" class="container">
<h1>Select text file</h1>
<input type="file" on-read-file="showContent($fileContent)" />
<div ng-if="content">
<h2>File content is:</h2>
<pre>{{ content }}</pre>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Javascript代码:
var myapp = angular.module('myapp', []);
myapp.controller('MainCtrl', function ($scope) {
$scope.showContent = function($fileContent) {
$scope.content = $fileContent;
};
});
myapp.directive('onReadFile', function ($parse) {
return {
restrict: 'A',
scope: false,
link: function(scope, element, attrs) {
var fn = $parse(attrs.onReadFile);
element.on('change', function(onChangeEvent) {
var reader = new FileReader();
reader.onload = function(onLoadEvent) {
scope.$apply(function() {
fn(scope, {$fileContent:onLoadEvent.target.result});
}); …Run Code Online (Sandbox Code Playgroud) javascript binaryfiles mongodb angularjs angularjs-directive
binaryfiles ×10
c ×2
file ×2
io ×2
python ×2
ada ×1
angularjs ×1
buffer ×1
c# ×1
c++ ×1
django-1.7 ×1
encoding ×1
fgetc ×1
filestream ×1
fread ×1
git ×1
javascript ×1
malloc ×1
mongodb ×1
python-3.4 ×1
r ×1
string ×1
ubuntu-9.10 ×1