我正在读取二进制文件,我想知道自定义对象的元素数量,二进制文件包含(这使得搜索EOF的大小毫无用处,因为它的所有数据都是关于数字的元素)。
这就是我现在正在做的事情:
using (var stream = File.OpenRead(openDialog.FileName))
using (var reader = new BinaryReader(stream))
{
while (CustObject.ReadFromBinaryReader(reader) != null)
{
objList.Add(CustObject.ReadFromBinaryReader(reader));
}
}
出于某种原因,这不起作用。我得到错误:Unable to read beyond end of stream.
所以相反,我想找出二进制文件中有多少元素,并使用该计数将所有对象读入对象列表。
我想使用的另一种方法是在写入文件时写入二进制文件时有多少元素。
binaryWriter.Write(string.Format("{0}", objList.Count()));
但是,我不知道如何阅读这个数字,所以我认为for
循环是最佳选择。
答案 0 :(得分:1)
data<-read.csv("Stericycle Waste Data.csv")
library(reshape2)
library(ggplot2)
library(plyr)
library(ggmap)
names(data)<-c("ID1","ID2", "Site.Address", "Type", "City", "Province", "Category", "Density", "Nov-14", "Dec-14", "Jan-15", "Feb-15", "Mar-15", "Apr-15", "May-15", "Jun-15", "Jul-15", "Aug-15", "Sep-15", "Oct-15", "Nov-15", "Dec-15", "Jan-16")
data<-melt(data, c("ID1","ID2", "Site.Address","Type", "City", "Province", "Category", "Density"))
data<-na.omit(data)
data_grouped<-ddply(data, c("Site.Address", "Type","City", "Province", "Category", "Density", "variable"), summarise, value=sum(value))
names(data_grouped)<-c("Site.Address", "Type", "City", "Province", "Category", "Density", "Month", 'Waste.Mass')
dummy<-read.csv('locations-coordinates.csv')
geodata<-merge(data_grouped, dummy, by.x="Site.Address", by.y="Site.Address", all.y=TRUE)
library(leaflet)
d = geodata_avg$density_factor
d = factor(d)
cols <- rainbow(length(levels(d)), alpha=NULL)
geodata_avg$colors <- cols[unclass(d)]
newmap <- leaflet(data=geodata_avg) %>% addTiles() %>%
addCircleMarkers(lng = ~lon, lat = ~lat, weight = 1, radius = ~rank*1.1, color = ~colors, popup = paste("Site Address: ", geodata_avg$Site.Address, "<br>", "Category: ", geodata_avg$Category, "<br>", "Average Waste: ", geodata_avg$value))
newmap
您的问题发生的原因可能是您在条件状态下阅读了最后一项时,之后再次阅读。
将文件开头的元素数写入整数,然后首先读取该单个字节。检查样本here如何将整数写入文件。然后先读取这个整数,继续阅读你的对象。
或者,如果您确切知道对象在文件中占用了多少字节(如果它有固定大小),请将文件大小除以此数字?